-
Notifications
You must be signed in to change notification settings - Fork 113
/
procdbase.c
336 lines (282 loc) · 7.33 KB
/
procdbase.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
** ATOP - System & Process Monitor
**
** The program 'atop' offers the possibility to view the activity of
** the system on system-level as well as process-level.
**
** This source-file contains all functions required to manipulate the
** process-database. This database is implemented as a linked list of
** all running processes, needed to remember the process-counters from
** the previous sample.
** ==========================================================================
** Author: Gerlof Langeveld
** E-mail: [email protected]
** Date: November 1996
** LINUX-port: June 2000
** --------------------------------------------------------------------------
** Copyright (C) 2000-2012 Gerlof Langeveld
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 2, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
** --------------------------------------------------------------------------
*/
#include <sys/types.h>
#include <sys/param.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#include "atop.h"
#include "photoproc.h"
/*****************************************************************************/
#define NPHASH 256 /* number of hash queues for process dbase */
/* MUST be a power of 2 !!! */
/* hash buckets for getting process-info */
/* for a given PID */
static struct pinfo *phash[NPHASH];
/* cyclic list of all processes, to detect */
/* which processes were not referred */
static struct pinfo presidue;
/*****************************************************************************/
/*
** search process database for the given PID
*/
int
pdb_gettask(int pid, char isproc, time_t btime, struct pinfo **pinfopp)
{
register struct pinfo *pp;
pp = phash[pid&(NPHASH-1)]; /* get proper hash bucket */
/*
** scan all entries in hash Q
*/
while (pp)
{
/*
** if this is required PID, unchain it from the RESIDUE-list
** and return info
*/
if (pp->tstat.gen.pid == pid &&
pp->tstat.gen.isproc == isproc )
{
int diff = pp->tstat.gen.btime - btime;
/*
** with longer intervals, the same PID might be
** found more than once, so also check the start
** time of the task
*/
if (diff > 1 || diff < -1)
{
pp = pp->phnext;
continue;
}
if (pp->prnext) /* if part of RESIDUE-list */
{
(pp->prnext)->prprev = pp->prprev; /* unchain */
(pp->prprev)->prnext = pp->prnext;
}
pp->prnext = NULL;
pp->prprev = NULL;
*pinfopp = pp;
return 1;
}
pp = pp->phnext;
}
/*
** end of list; PID not found
*/
return 0;
}
/*
** add new process-info structure to the process database
*/
void
pdb_addtask(int pid, struct pinfo *pinfop)
{
register int i = pid&(NPHASH-1);
pinfop->phnext = phash[i];
phash[i] = pinfop;
}
/*
** delete a process from the process database
*/
int
pdb_deltask(int pid, char isproc)
{
register struct pinfo *pp, *ppp;
pp = phash[pid&(NPHASH-1)]; /* get proper hash bucket */
/*
** check first entry in hash Q
*/
if (pp->tstat.gen.pid == pid && pp->tstat.gen.isproc == isproc)
{
phash[pid&(NPHASH-1)] = pp->phnext;
if ( pp->prnext ) /* still part of RESIDUE-list ? */
{
(pp->prprev)->prnext = pp->prnext;
(pp->prnext)->prprev = pp->prprev; /* unchain */
}
/*
** remove process-info from process-database
*/
free(pp);
return 1;
}
/*
** scan other entries of hash-list
*/
ppp = pp;
pp = pp->phnext;
while (pp)
{
/*
** if this is wanted PID, unchain it from the RESIDUE-list
** and return info
*/
if (pp->tstat.gen.pid == pid && pp->tstat.gen.isproc == isproc)
{
ppp->phnext = pp->phnext;
if ( pp->prnext ) /* part of RESIDUE-list ? */
{
(pp->prnext)->prprev = pp->prprev;
(pp->prprev)->prnext = pp->prnext;
}
/*
** remove process-info from process-database
*/
free(pp);
return 1;
}
ppp = pp;
pp = pp->phnext;
}
return 0; /* PID not found */
}
/*
** Chain all process-info structures into the RESIDUE-list;
** every process-info struct which is referenced later on by pdb_gettask(),
** will be removed from this list again. After that, the remaining
** (unreferred) process-info structs can be easily discovered and
** eventually removed.
*/
int
pdb_makeresidue(void)
{
register struct pinfo *pp, *pr;
register int i;
/*
** prepare RESIDUE-list anchor
*/
pr = &presidue;
pr->prnext = pr;
pr->prprev = pr;
/*
** check all entries in hash list
*/
for (i=0; i < NPHASH; i++)
{
if (!phash[i])
continue; /* empty hash bucket */
pp = phash[i]; /* get start of list */
while (pp) /* all entries in hash list */
{
pp->prnext = pr->prnext;
pr->prnext = pp;
pp->prprev = (pp->prnext)->prprev;
(pp->prnext)->prprev = pp;
pp = pp->phnext; /* get next of hash list */
}
}
/*
** all entries chained in doubly-linked RESIDUE-list
*/
return 1;
}
/*
** remove all remaining entries in RESIDUE-list
*/
int
pdb_cleanresidue(void)
{
register struct pinfo *pr;
register int pid;
char isproc;
/*
** start at RESIDUE-list anchor and delete all entries
*/
pr = presidue.prnext;
while (pr != &presidue)
{
pid = pr->tstat.gen.pid;
isproc = pr->tstat.gen.isproc;
pr = pr->prnext; /* MUST be done before deletion */
pdb_deltask(pid, isproc);
}
return 1;
}
/*
** search in the RESIDUE-list for process-info which may fit to the
** given process-info, for which the PID is not known
*/
int
pdb_srchresidue(struct tstat *tstatp, struct pinfo **pinfopp)
{
register struct pinfo *pr, *prmin=NULL;
register long btimediff;
/*
** start at RESIDUE-list anchor and search through
** all remaining entries
*/
pr = presidue.prnext;
while (pr != &presidue) /* still entries left ? */
{
/*
** check if this entry matches searched info
*/
if ( pr->tstat.gen.ruid == tstatp->gen.ruid &&
pr->tstat.gen.rgid == tstatp->gen.rgid &&
strcmp(pr->tstat.gen.name, tstatp->gen.name) == EQ )
{
/*
** check if the start-time of the process is exactly
** the same ----> then we have a match;
** however sometimes the start-time may deviate a
** second although it IS the process we are looking
** for (depending on the rounding of the boot-time),
** so if we don't find the exact match, we will check
** later on if we found an almost-exact match
*/
btimediff = pr->tstat.gen.btime - tstatp->gen.btime;
if (btimediff == 0) /* gotcha !! */
{
*pinfopp = pr;
return 1;
}
if ((btimediff== -1 || btimediff== 1) && prmin== NULL)
prmin = pr; /* remember this process */
}
pr = pr->prnext;
}
/*
** nothing found that matched exactly;
** do we remember a process that matched almost exactly?
*/
if (prmin)
{
*pinfopp = prmin;
return 1;
}
return 0; /* even not almost */
}