File: ipc_utils.cpp

package info (click to toggle)
onetbb 2022.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,444 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (140 lines) | stat: -rw-r--r-- 3,477 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
    Copyright (c) 2017-2021 Intel Corporation

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "ipc_utils.h"

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>

namespace tbb {
namespace internal {
namespace rml {

#define MAX_STR_LEN 255
#define STARTTIME_ITEM_ID 21

static char* get_stat_item(char* line, int item_id) {
    int id = 0, i = 0;

    while( id!=item_id ) {
        while( line[i]!='(' && line[i]!=' ' && line[i]!='\0' ) {
            ++i;
        }
        if( line[i]==' ' ) {
            ++id;
            ++i;
        } else if( line[i]=='(' ) {
            while( line[i]!=')' && line[i]!='\0' ) {
               ++i;
            }
            if( line[i]==')' ) {
                ++i;
            } else {
                return nullptr;
            }
        } else {
            return nullptr;
        }
    }

    return line + i;
}

unsigned long long get_start_time(int pid) {
    const char* stat_file_path_template = "/proc/%d/stat";
    char stat_file_path[MAX_STR_LEN + 1];
    sprintf( stat_file_path, stat_file_path_template, pid );

    FILE* stat_file = fopen( stat_file_path, "rt" );
    if( stat_file==nullptr ) {
        return 0;
    }

    char stat_line[MAX_STR_LEN + 1];
    char* line = fgets( stat_line, MAX_STR_LEN, stat_file );
    if( line==nullptr ) {
        return 0;
    }

    char* starttime_str = get_stat_item( stat_line, STARTTIME_ITEM_ID );
    if( starttime_str==nullptr ) {
        return 0;
    }

    unsigned long long starttime = strtoull( starttime_str, nullptr, 10 );
    if( starttime==ULLONG_MAX ) {
        return 0;
    }

    return starttime;
}

char* get_shared_name(const char* prefix, int pid, unsigned long long time) {
    const char* name_template = "%s_%d_%llu";
    const int digits_in_int = 10;
    const int digits_in_long = 20;

    int len = strlen( name_template ) + strlen( prefix ) + digits_in_int + digits_in_long + 1;
    char* name = new char[len];
    sprintf( name, name_template, prefix, pid, time );

    return name;
}

char* get_shared_name(const char* prefix) {
    int pid = getpgrp();
    unsigned long long time = get_start_time( pid );
    return get_shared_name( prefix, pid, time );
}

int get_num_threads(const char* env_var) {
    if( env_var==nullptr ) {
        return 0;
    }

    char* value = getenv( env_var );
    if( value==nullptr ) {
        return 0;
    }

    int num_threads = (int)strtol( value, nullptr, 10 );
    return num_threads;
}

bool get_enable_flag(const char* env_var) {
    if( env_var==nullptr ) {
        return false;
    }

    char* value = getenv( env_var );
    if( value==nullptr ) {
        return false;
    }

    if( strcmp( value, "0" ) == 0 ||
        strcmp( value, "false" ) == 0 ||
        strcmp( value, "False" ) == 0 ||
        strcmp( value, "FALSE" ) == 0 ) {
        return false;
    }

    return true;
}

}}} // namespace tbb::internal::rml