-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnightswatch.c
163 lines (153 loc) · 3.33 KB
/
nightswatch.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
#include "header.h"
bool qPressed()
{
fd_set fd;
FD_SET(0, &fd);
struct timeval tVal;
tVal.tv_sec = 0;
tVal.tv_usec = 0;
int selRet = select(1, &fd, NULL, NULL, &tVal);
if (selRet > 0)
{
char c = fgetc(stdin);
if (c == EOF)
return false;
if (c == 'q')
{
fgetc(stdin); //for enter
return true;
}
}
else if (selRet < 0)
{
perror("select() for watching if q");
return true;
}
else
{
return false;
}
return false;
}
int interr(int timeInt)
{
long noOfThreads = sysconf(_SC_NPROCESSORS_ONLN);
char buffer[1000];
for (int i = 0; i < noOfThreads; i++)
printf("CPU%d\t", i);
printf("\n");
char *itups[noOfThreads + 5];
int cnt = 0;
FILE *fd;
while (true)
{
fd = fopen("/proc/interrupts", "r");
if (fd == NULL)
{
perror("nightswatch interrupt: /proc/interrupts");
return 1;
}
int i = 0;
while (fgets(buffer, 1000, fd) != NULL && i++ < 3)
{
if (i != 3)
continue;
if (i == 3)
{
// puts(buffer);
fflush(NULL);
int j = 0;
itups[0] = strtok(buffer, " \t\n");
while (j < noOfThreads)
{
// puts(itups[j]);
itups[++j] = strtok(NULL, " \t\n");
}
}
for (int k = 1; k <= noOfThreads; k++)
{
printf("%s\t", itups[k]);
}
printf("\n");
}
if (qPressed())
return 1;
sleep(timeInt);
if (qPressed())
return 1;
}
return 1;
}
int newBorn(int timeInt)
{
bool run = true;
int i = 0;
int pid;
// printf("in newborn\n");
while (run)
{
FILE *fd = fopen("/proc/loadavg", "r");
if (fd == NULL)
{
perror("Nightswatch newborn read /proc/loadavg");
return -1;
}
fscanf(fd, "%*s %*s %*s %*s %d", &pid);
printf("%d\n", pid);
if (qPressed())
break;
sleep(timeInt);
if (qPressed())
break;
fclose(fd);
}
return 1;
}
int nightswatch(int numPar, char *par[])
{
// printf("timeInt: %d\n", timeInt);
if (numPar == 3 && strcmp(par[0], "-n") == 0)
{
int timeInt = atoi(par[1]);
if (strcmp(par[2], "interrupt") == 0)
{
return interr(timeInt);
}
else if (strcmp(par[2], "newborn") == 0)
{
return newBorn(timeInt);
// printf("ayee\n");
}
else
{
printf("wrong input\n");
return -1;
}
}
else if (numPar == 3)
{
printf("Wrong input\n");
return -1;
}
else if (numPar == 1)
{
if (strcmp(par[0], "interrupt") == 0)
{
return interr(1);
}
else if (strcmp(par[0], "newborn") == 0)
{
return newBorn(1);
// printf("ayee\n");
}
else
{
printf("wrong input\n");
return -1;
}
}
else {
printf("Wrong input\n");
return -1;
}
}