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
|
/**
*
* cpulimit - a cpu limiter for Linux
*
* Copyright (C) 2005-2008, by: Angelo Marletta <marlonx80@hotmail.com>
*
* 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
* of the License, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//TODO: add documentation to public functions
#include "process.h"
#ifdef __APPLE__
#include <Carbon/Carbon.h>
#endif
int process_init(struct process_history *proc, int pid)
{
#ifdef __linux__
//test /proc file descriptor for reading
sprintf(proc->stat_file, "/proc/%d/stat", pid);
FILE *fd = fopen(proc->stat_file, "r");
if (fd == NULL) return 1;
fclose(fd);
#endif
//init properties
proc->pid = pid;
proc->cpu_usage = 0;
memset(&(proc->last_sample), 0, sizeof(struct timeval));
proc->last_jiffies = -1;
return 0;
}
//return t1-t2 in microseconds (no overflow checks, so better watch out!)
static inline unsigned long timediff(const struct timeval *t1,const struct timeval *t2)
{
return (t1->tv_sec - t2->tv_sec) * 1000000 + (t1->tv_usec - t2->tv_usec);
}
static int get_jiffies(struct process_history *proc) {
#ifdef __linux__
FILE *f = fopen(proc->stat_file, "r");
if (f==NULL) return -1;
fgets(proc->buffer, sizeof(proc->buffer),f);
fclose(f);
char *p = proc->buffer;
p = memchr(p+1,')', sizeof(proc->buffer) - (p-proc->buffer));
int sp = 12;
while (sp--)
p = memchr(p+1,' ',sizeof(proc->buffer) - (p-proc->buffer));
//user mode jiffies
int utime = atoi(p+1);
p = memchr(p+1,' ',sizeof(proc->buffer) - (p-proc->buffer));
//kernel mode jiffies
int ktime = atoi(p+1);
return utime+ktime;
#elif defined __APPLE__
ProcessSerialNumber psn;
ProcessInfoRec info;
if (GetProcessForPID(proc->pid, &psn)) return -1;
if (GetProcessInformation(&psn, &info)) return -1;
return info.processActiveTime;
#endif
}
//parameter in range 0-1
#define ALFA 0.08
int process_monitor(struct process_history *proc)
{
int j = get_jiffies(proc);
if (j<0) return -1; //error retrieving jiffies count (maybe the process is dead)
struct timeval now;
gettimeofday(&now, NULL);
if (proc->last_jiffies==-1) {
//store current time
proc->last_sample = now;
//store current jiffies
proc->last_jiffies = j;
//it's the first sample, cannot figure out the cpu usage
proc->cpu_usage = -1;
return 0;
}
//time from previous sample (in ns)
long dt = timediff(&now, &(proc->last_sample));
//how many jiffies in dt?
double max_jiffies = dt * HZ / 1000000.0;
double sample = (j - proc->last_jiffies) / max_jiffies;
if (proc->cpu_usage == -1) {
//initialization
proc->cpu_usage = sample;
}
else {
//usage adjustment
proc->cpu_usage = (1-ALFA) * proc->cpu_usage + ALFA * sample;
}
//store current time
proc->last_sample = now;
//store current jiffies
proc->last_jiffies = j;
return 0;
}
int process_close(struct process_history *proc)
{
if (kill(proc->pid,SIGCONT)!=0) {
fprintf(stderr,"Process %d is already dead!\n", proc->pid);
}
proc->pid = 0;
return 0;
}
|