-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocstat.c
266 lines (219 loc) · 8.73 KB
/
procstat.c
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
266
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "procstat.h"
struct ProcStatusData* get_context_swaps_for_process(pid_t *id){
pid_t the_id = 0;
if(id == NULL)
the_id = getpid();
else
the_id = *id;
struct ProcStatusData *data = malloc(sizeof(struct ProcStatusData));
if(data == NULL){
perror("Failed to initialize ProcStatusData struct!!");
exit( EXIT_FAILURE );
}
memset(data, 0, sizeof(struct ProcStatusData));
const size_t path_buffer_size = 500;
char path_buffer[ path_buffer_size ];
memset(path_buffer, '\0', path_buffer_size * sizeof( char ) );
snprintf(path_buffer, path_buffer_size, "/proc/%d/status", the_id);
FILE *fp = fopen(path_buffer, "r");
if(fp == NULL){
perror(path_buffer);
exit(EXIT_FAILURE);
}
const size_t token_buffer_size = 100;
char token_buffer[ token_buffer_size ];
memset(token_buffer, '\0', token_buffer_size );
int num_read = fscanf(fp, "%s\n",token_buffer);
enum STATE {
VOL = 0,
NON,
DONE,
N
};
const char *tokens[N] =
{
[VOL] = "voluntary_ctxt_switches:",
[NON] = "nonvoluntary_ctxt_switches:",
[DONE] = NULL
};
enum STATE s = VOL;
while(EOF != num_read ){
if(strcmp(token_buffer, tokens[s]) == 0){
num_read = fscanf(fp, "%s\n",token_buffer);
if(s == VOL) data->voluntary_context_swaps =
strtol(token_buffer, NULL, 10);
if(s == NON) data->non_voluntary_context_swaps =
strtol(token_buffer, NULL, 10);
if(++s == DONE) break;
}
num_read = fscanf(fp, "%s\n",token_buffer);
}
fclose(fp);
return(data);
};
struct ProcStatData* get_proc_stat_data_for_process(pid_t *pid){
pid_t the_id = 0;
if(pid == NULL)
the_id = getpid();
else
the_id = *pid;
struct ProcStatData *data = malloc(sizeof(struct ProcStatData));
if(data == NULL){
perror("Failed to initialize ProcStatData struct!!");
exit(EXIT_FAILURE);
}
memset(data, 0, sizeof(struct ProcStatData));
const size_t path_buffer_size = 500;
char path_buffer[ path_buffer_size ];
memset( path_buffer, '\0', path_buffer_size * sizeof( char ) );
snprintf(path_buffer, path_buffer_size, "/proc/%d/stat", the_id );
FILE *fp = fopen(path_buffer, "r");
if(fp == NULL){
perror(path_buffer);
exit(EXIT_FAILURE);
}
const size_t token_buffer_size = 100;
char token_buffer[ token_buffer_size ];
memset( token_buffer, '\0', token_buffer_size * sizeof( char ) );
/* should probably also check errno and re-add the null term for
* strings if this is to be used in production code */
//int pid;
fscanf(fp, "%s",token_buffer);
data->pid = strtol(token_buffer,NULL,10);
//char executable[100];
fscanf(fp, "%s",token_buffer);
{
const size_t length = strlen(token_buffer);
strncpy(data->executable,token_buffer, length);
}
//char state;
fscanf(fp, "%s",token_buffer);
data->state = token_buffer[0];
//int parent_pid;
fscanf(fp, "%s",token_buffer);
data->parent_pid = strtol(token_buffer,NULL,10);
//int group_id;
fscanf(fp, "%s",token_buffer);
data->group_id = strtol(token_buffer,NULL,10);
//int session_id;
fscanf(fp, "%s",token_buffer);
data->session_id = strtol(token_buffer, NULL, 10);
//int tty_nr; /* controlling terminal */
fscanf(fp, "%s",token_buffer);
data->tty_nr = strtol(token_buffer, NULL, 10);
//int foreground_id; /* id of foreground process group of controlling terminal process */
fscanf(fp, "%s",token_buffer);
data->foreground_id = strtol(token_buffer, NULL, 10);
//unsigned int flags;
fscanf(fp, "%s",token_buffer);
data->flags = strtoul(token_buffer, NULL, 10);
//long unsigned int minor_faults;
fscanf(fp, "%s",token_buffer);
data->minor_faults = strtoul(token_buffer, NULL, 10);
//long unsigned int child_minor_faults;
fscanf(fp, "%s",token_buffer);
data->child_minor_faults = strtoul(token_buffer, NULL, 10);
//long unsigned int major_faults;
fscanf(fp, "%s",token_buffer);
data->major_faults = strtoul(token_buffer, NULL, 10);
//long unsigned int child_major_faults;
fscanf(fp, "%s",token_buffer);
data->child_major_faults = strtoul(token_buffer, NULL, 10);
//long unsigned int uptime;
fscanf(fp, "%s",token_buffer);
data->uptime = strtoul(token_buffer, NULL, 10);
//long unsigned int scheduled_time; /* in clock ticks, divide by sysconf(__SC_CLK_TCK) */
fscanf(fp, "%s",token_buffer);
data->scheduled_time = strtoul(token_buffer, NULL, 10);
//long int child_uptime;
fscanf(fp, "%s",token_buffer);
data->child_uptime = strtol(token_buffer,NULL,10);
//long int child_scheduled_time;
fscanf(fp, "%s",token_buffer);
data->child_scheduled_time = strtol(token_buffer, NULL, 10);
//long int priority;
fscanf(fp, "%s",token_buffer);
data->priority = strtol(token_buffer,NULL,10);
//long int nice;
fscanf(fp, "%s",token_buffer);
data->nice = strtol(token_buffer, NULL, 10);
//long int number_threads;
fscanf(fp, "%s",token_buffer);
data->number_threads = strtol(token_buffer, NULL, 10);
//long int itrealvalue; /* not used */
fscanf(fp, "%s",token_buffer);
data->itrealvalue = strtol(token_buffer, NULL, 10);
//long long unsigned int start_time; /* in jiffies */
fscanf(fp, "%s",token_buffer);
data->start_time = strtoull(token_buffer, NULL, 10);
//long unsigned int virtual_mem_size_bytes;
fscanf(fp, "%s",token_buffer);
data->virtual_mem_size_bytes = strtoul(token_buffer, NULL, 10);
//long int resident_mem_size;
fscanf(fp, "%s",token_buffer);
data->resident_mem_size = strtol(token_buffer, NULL, 10);
//long unsigned int resident_mem_soft_limit;
fscanf(fp, "%s",token_buffer);
data->resident_mem_soft_limit= strtol(token_buffer,NULL, 10);
//long unsigned int startcode; /* address above which program text can run */
fscanf(fp, "%s",token_buffer);
data->startcode = strtoul(token_buffer, NULL, 10);
//long unsigned int endcode; /* address below which program text can run */
fscanf(fp, "%s",token_buffer);
data->endcode = strtoul(token_buffer, NULL, 10);
//long unsigned int startstack; /* bottom of stack */
fscanf(fp, "%s",token_buffer);
data->startstack = strtoul(token_buffer, NULL, 10);
//long unsigned int curr_esp; /* current value of stack pointer */
fscanf(fp, "%s",token_buffer);
data->curr_esp = strtoul(token_buffer, NULL, 10);
//long unsigned int curr_eip; /* current instruction pointer */
fscanf(fp, "%s",token_buffer);
data->curr_eip = strtoul(token_buffer, NULL, 10);
//long unsigned int signal_unused;
fscanf(fp, "%s",token_buffer);
data->signal_unused = strtoul(token_buffer, NULL, 10);
// long unsigned int signal_ignore_unused;
fscanf(fp, "%s",token_buffer);
data->signal_ignore_unused = strtoul(token_buffer, NULL, 10);
// long unsigned int signal_caught_unused;
fscanf(fp, "%s",token_buffer);
data->signal_caught_unused = strtoul(token_buffer, NULL, 10);
//long unsigned int channel;
fscanf(fp, "%s",token_buffer);
data->channel = strtoul(token_buffer, NULL, 10);
//long unsigned int pages_swapped;
fscanf(fp, "%s",token_buffer);
data->pages_swapped = strtoul(token_buffer, NULL, 10);
//long unsigned int cumulative_child_swapped_pages;
fscanf(fp, "%s",token_buffer);
data->cumulative_child_swapped_pages = strtoul(token_buffer, NULL, 10);
//int exit_signal;
fscanf(fp, "%s",token_buffer);
data->exit_signal = strtol(token_buffer, NULL, 10);
//int processor_last_executed_on;
fscanf(fp, "%s",token_buffer);
data->processor_last_executed_on = strtol(token_buffer, NULL, 10);
//unsigned int rt_schedule; /* 0-99 for RT process, otherwise 0 */
fscanf(fp, "%s",token_buffer);
data->rt_schedule = strtoul(token_buffer, NULL, 10);
//unsigned int policy; /* defined in linux/sched.h */
fscanf(fp, "%s",token_buffer);
data->policy = strtoul(token_buffer, NULL, 10);
//long long unsigned int delayed_io_ticks; /* clock ticks process was delayed for io */
fscanf(fp, "%s",token_buffer);
data->delayed_io_ticks = strtoull(token_buffer, NULL, 10);
//long unsigned int guest_time; /* time spent running a virtual cpu for guest OS */
fscanf(fp, "%s",token_buffer);
data->guest_time = strtoul(token_buffer, NULL, 10);
//long int child_guest_time;
fscanf(fp, "%s",token_buffer);
data->child_guest_time = strtol(token_buffer, NULL, 10);
fclose(fp);
return(data);
};