File: pstat_linux_module.c

package info (click to toggle)
openmpi 1.6.5-9.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 91,628 kB
  • ctags: 44,305
  • sloc: ansic: 408,966; cpp: 44,454; sh: 27,828; makefile: 10,486; asm: 3,882; python: 1,239; lex: 805; perl: 549; csh: 253; fortran: 232; f90: 126; tcl: 12
file content (265 lines) | stat: -rw-r--r-- 7,665 bytes parent folder | download | duplicates (2)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Copyright (c) 2004-2008 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2006-2007 Cisco Systems, Inc.  All rights reserved.
 *
 * $COPYRIGHT$
 * 
 * Additional copyrights may follow
 * 
 * $HEADER$
 */

#include "opal_config.h"
#include "opal/constants.h"

/* This component will only be compiled on Linux, where we are
   guaranteed to have <unistd.h> and friends */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>

#include <sys/param.h>  /* for HZ to convert jiffies to actual time */

#include "opal/mca/base/mca_base_param.h"
#include "opal/dss/dss_types.h"
#include "opal/util/printf.h"

#include "pstat_linux.h"

/*
 * Local functions
 */
static int linux_module_init(void);
static int query(pid_t pid, opal_pstats_t *stats);
static int linux_module_fini(void);

/*
 * Linux pstat module
 */
const opal_pstat_base_module_t opal_pstat_linux_module = {
    /* Initialization function */
    linux_module_init,
    query,
    linux_module_fini
};

static int linux_module_init(void)
{
    return OPAL_SUCCESS;
}

static int linux_module_fini(void)
{
    return OPAL_SUCCESS;
}

static char *next_field(char *ptr, int barrier)
{
    int i=0;
    
    /* we are probably pointing to the last char
     * of the current field, so look for whitespace
     */
    while (!isspace(*ptr) && i < barrier) {
        ptr++;  /* step over the current char */
        i++;
    }
    
    /* now look for the next field */
    while (isspace(*ptr) && i < barrier) {
        ptr++;
        i++;
    }
    
    return ptr;
}


static int query(pid_t pid, opal_pstats_t *stats)
{
    char data[4096];
    int fd;
    size_t numchars;
    char *ptr, *eptr;
    int i;
    int len;
    
    /* create the stat filename for this proc */
    numchars = snprintf(data, sizeof(data), "/proc/%d/stat", pid);
    if (numchars >= sizeof(data)) {
        return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
    }
    
    if (0 > (fd = open(data, O_RDONLY))) {
        /* can't access this file - most likely, this means we
         * aren't really on a supported system, or the proc no
         * longer exists. Just return an error
         */
        return OPAL_ERR_FILE_OPEN_FAILURE;
    }
    
    /* absorb all of the file's contents in one gulp - we'll process
     * it once it is in memory for speed
     */
    memset(data, 0, sizeof(data));
    len = read(fd, data, sizeof(data)-1);
    if (len < 0) {
        /* This shouldn't happen! */
        return OPAL_ERR_FILE_OPEN_FAILURE;
    }
    close(fd);
    
    /* remove newline at end */
    data[len] = '\0';
    
    /* the stat file consists of a single line in a carefully formatted
     * form. Parse it field by field as per proc(3) to get the ones we want
     */
    
    /* we don't need to read the pid from the file - we already know it! */
    stats->pid = pid;
    
    /* the cmd is surrounded by parentheses - find the start */
    if (NULL == (ptr = strchr(data, '('))) {
        /* no cmd => something wrong with data, return error */
        return OPAL_ERR_BAD_PARAM;
    }
    /* step over the paren */
    ptr++;
    
    /* find the ending paren */
    if (NULL == (eptr = strchr(ptr, ')'))) {
        /* no end to cmd => something wrong with data, return error */
        return OPAL_ERR_BAD_PARAM;
    }
    
    /* save the cmd name, up to the limit of the array */
    i = 0;
    while (ptr < eptr && i < OPAL_PSTAT_MAX_STRING_LEN) {
        stats->cmd[i++] = *ptr++;
    }
    
    /* move to the next field in the data */
    ptr = next_field(eptr, len);
    
    /* next is the process state - a single character */
    stats->state = *ptr;
    /* move to next field */
    ptr = next_field(ptr, len);
    
    /* skip fields until we get to the times */
    ptr = next_field(ptr, len); /* ppid */
    ptr = next_field(ptr, len); /* pgrp */
    ptr = next_field(ptr, len); /* session */
    ptr = next_field(ptr, len); /* tty_nr */
    ptr = next_field(ptr, len); /* tpgid */
    ptr = next_field(ptr, len); /* flags */
    ptr = next_field(ptr, len); /* minflt */
    ptr = next_field(ptr, len); /* cminflt */
    ptr = next_field(ptr, len); /* majflt */
    ptr = next_field(ptr, len); /* cmajflt */
    
    /* grab the process time usage fields */
    stats->time = strtoul(ptr, &ptr, 10);    /* utime */
    stats->time += strtoul(ptr, &ptr, 10);   /* add the stime */
    stats->time = stats->time / HZ;  /* convert to time */
    /* move to next field */
    ptr = next_field(ptr, len);
    
    /* skip fields until we get to priority */
    ptr = next_field(ptr, len); /* cutime */
    ptr = next_field(ptr, len); /* cstime */
    
    /* save the priority */
    stats->priority = strtol(ptr, &ptr, 10);
    
    /* that's all we care about from this data - ignore the rest */
    
    /* now create the status filename for this proc */
    memset(data, 0, sizeof(data));
    numchars = snprintf(data, sizeof(data), "/proc/%d/status", pid);
    if (numchars >= sizeof(data)) {
        return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
    }
    
    if (0 > (fd = open(data, O_RDONLY))) {
        /* can't access this file - most likely, this means we
         * aren't really on a supported system, or the proc no
         * longer exists. Just return an error
         */
        return OPAL_ERR_FILE_OPEN_FAILURE;
    }
    
    /* absorb all of the file's contents in one gulp - we'll process
     * it once it is in memory for speed
     */
    memset(data, 0, sizeof(data));
    len = read(fd, data, sizeof(data)-1);
    close(fd);
    
    /* remove newline at end */
    data[len] = '\0';
    
    /* parse it according to proc(3) */
    eptr = data;
    /* look for VmPeak */
    if (NULL != (ptr = strstr(data, "VmPeak:"))) {
        /* found it - step past colon */
        ptr += 8;
        eptr = strchr(ptr, 'k');
        *eptr = '\0';
        stats->peak_vsize = strtoul(ptr, NULL, 10);  /* already in kB */
        eptr++;
    }
     /* look for VmSize */
    if (NULL != (ptr = strstr(eptr, "VmSize:"))) {
        /* found it - step past colon */
        ptr += 8;
        eptr = strchr(ptr, 'k');
        *eptr = '\0';
        stats->vsize = strtoul(ptr, NULL, 10);  /* already in kB */
        eptr++;
    }
    
    /* look for RSS */
    if (NULL != (ptr = strstr(eptr, "VmRSS:"))) {
        /* found it - step past colon */
        ptr += 8;
        eptr = strchr(ptr, 'k');
        *eptr = '\0';
        stats->rss = strtoul(ptr, NULL, 10);  /* already in kB */
        eptr++;
    }

    /* look for Libraries */
    if (NULL != (ptr = strstr(eptr, "VmLib:"))) {
        /* found it - step past colon */
        ptr += 8;
        eptr = strchr(ptr, 'k');
        *eptr = '\0';
        stats->shared_size = strtoul(ptr, NULL, 10);  /* already in kB */
        eptr++;
    }

    /* look for threads */
    if (NULL != (ptr = strstr(eptr, "Threads:"))) {
        /* found it - step past colon */
        ptr += 8;
        stats->num_threads = strtoul(ptr, NULL, 10);
    }
    
    return OPAL_SUCCESS;
}