1 /*
2 Copyright (c) 2017-2021 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 #include "ipc_utils.h"
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <limits.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 namespace tbb {
26 namespace internal {
27 namespace rml {
28
29 #define MAX_STR_LEN 255
30 #define STARTTIME_ITEM_ID 21
31
get_stat_item(char * line,int item_id)32 static char* get_stat_item(char* line, int item_id) {
33 int id = 0, i = 0;
34
35 while( id!=item_id ) {
36 while( line[i]!='(' && line[i]!=' ' && line[i]!='\0' ) {
37 ++i;
38 }
39 if( line[i]==' ' ) {
40 ++id;
41 ++i;
42 } else if( line[i]=='(' ) {
43 while( line[i]!=')' && line[i]!='\0' ) {
44 ++i;
45 }
46 if( line[i]==')' ) {
47 ++i;
48 } else {
49 return nullptr;
50 }
51 } else {
52 return nullptr;
53 }
54 }
55
56 return line + i;
57 }
58
get_start_time(int pid)59 unsigned long long get_start_time(int pid) {
60 const char* stat_file_path_template = "/proc/%d/stat";
61 char stat_file_path[MAX_STR_LEN + 1];
62 sprintf( stat_file_path, stat_file_path_template, pid );
63
64 FILE* stat_file = fopen( stat_file_path, "rt" );
65 if( stat_file==nullptr ) {
66 return 0;
67 }
68
69 char stat_line[MAX_STR_LEN + 1];
70 char* line = fgets( stat_line, MAX_STR_LEN, stat_file );
71 if( line==nullptr ) {
72 return 0;
73 }
74
75 char* starttime_str = get_stat_item( stat_line, STARTTIME_ITEM_ID );
76 if( starttime_str==nullptr ) {
77 return 0;
78 }
79
80 unsigned long long starttime = strtoull( starttime_str, nullptr, 10 );
81 if( starttime==ULLONG_MAX ) {
82 return 0;
83 }
84
85 return starttime;
86 }
87
get_shared_name(const char * prefix,int pid,unsigned long long time)88 char* get_shared_name(const char* prefix, int pid, unsigned long long time) {
89 const char* name_template = "%s_%d_%llu";
90 const int digits_in_int = 10;
91 const int digits_in_long = 20;
92
93 int len = strlen( name_template ) + strlen( prefix ) + digits_in_int + digits_in_long + 1;
94 char* name = new char[len];
95 sprintf( name, name_template, prefix, pid, time );
96
97 return name;
98 }
99
get_shared_name(const char * prefix)100 char* get_shared_name(const char* prefix) {
101 int pid = getpgrp();
102 unsigned long long time = get_start_time( pid );
103 return get_shared_name( prefix, pid, time );
104 }
105
get_num_threads(const char * env_var)106 int get_num_threads(const char* env_var) {
107 if( env_var==nullptr ) {
108 return 0;
109 }
110
111 char* value = getenv( env_var );
112 if( value==nullptr ) {
113 return 0;
114 }
115
116 int num_threads = (int)strtol( value, nullptr, 10 );
117 return num_threads;
118 }
119
get_enable_flag(const char * env_var)120 bool get_enable_flag(const char* env_var) {
121 if( env_var==nullptr ) {
122 return false;
123 }
124
125 char* value = getenv( env_var );
126 if( value==nullptr ) {
127 return false;
128 }
129
130 if( strcmp( value, "0" ) == 0 ||
131 strcmp( value, "false" ) == 0 ||
132 strcmp( value, "False" ) == 0 ||
133 strcmp( value, "FALSE" ) == 0 ) {
134 return false;
135 }
136
137 return true;
138 }
139
140 }}} // namespace tbb::internal::rml
141