-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_nightswatch_prop_nc.c
More file actions
145 lines (127 loc) · 4.02 KB
/
exec_nightswatch_prop_nc.c
File metadata and controls
145 lines (127 loc) · 4.02 KB
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
#include "shell.h"
/**
* STEPS
* 1. find the number of cores in the laptop
* 2. then somehow figure out how to find the number of interrupts(done)
* 3. then wait for n seconds or exit on pressing d
*/
int debugg = 0;
void exec_nightswatch(char *arg, char **args)
{
char buffer[MAX_BUFFER_LENGTH];
char *line = (char *)malloc(sizeof(char) * MAX_LINE_LENGTH);
if (!line)
memory_error();
if (!(args[1] != NULL && strcmp(args[1], "-n") == 0 && args[3] != NULL && ((strcmp(args[3], "interrupt") == 0 || strcmp(args[3], "dirty") == 0)) && (args[4] == NULL || args[4][0] == '\0')))
{
print_n("Usage: nightswatch -n (time in seconds) (interrupt | dirty) ");
return;
}
int frequency = atoi(args[2]);
if (frequency == 0)
{
print_n("Usage: nightswatch -n (time in seconds) (interrupt | dirty) ");
return;
}
//find the number of cores
size_t sz = 0;
int no_of_cores = 0;
FILE *core = fopen("/proc/cpuinfo", "r");
if (core == NULL)
{
perror("cpuinfo: ");
return;
}
while (getline(&line, &sz, core) != -1)
{
if (strncmp(line, "cpu cores", 9) == 0)
{
if (debugg)
print_n(line);
int cur = 0;
for (int i = 11; line[i] != '\0'; i++, cur++)
buffer[cur] = line[i];
buffer[cur] = '\0';
no_of_cores = atoi(buffer);
break;
}
}
fclose(core);
// Timer function
fd_set input_set;
struct timeval timeout;
FD_ZERO(&input_set);
FD_SET(STDIN_FILENO, &input_set);
if (strcmp(args[3], "interrupt") == 0)
{
print("0\tCPU0\tCPU1");
int k = 1;
while (1)
{
FILE *interrupt = fopen("/proc/interrupts", "r");
ssize_t reads;
size_t len = 0;
char *line = NULL;
if (interrupt == NULL)
{
perror("Error opening interrupt file: ");
return;
}
long long int cpu[100] = {0};
long long int read_cpu[100] = {0};
getline(&line, &len, interrupt);
while (getline(&line, &len, interrupt) != -1)
{
//check if the line ends with i8042
char *p = line;
while (*p == ' ' || *p == '\t')
p++;
while (*p != ':')
p++; //Column 1 is not required
for (int reading_cpu_no = 0; reading_cpu_no < no_of_cores; reading_cpu_no++)
{
while (*p == ' ' || *p == '\t')
p++;
int cur = 0;
while (*p != ' ' && *p != '\t')
buffer[cur++] == *p++;
buffer[cur] = '\0';
read_cpu[reading_cpu_no] = atoi(buffer);
}
}
sscanf(line, "%*lld: %lld %lld", &cpu[0], &cpu[1]);
printf("%lld\t%lld\n", cpu[0], cpu[1]);
k++;
fclose(interrupt);
timeout.tv_sec = frequency; // frequency seconds
timeout.tv_usec = 0; // 0 milliseconds
select(1, &input_set, NULL, NULL, &timeout);
}
}
if (strcmp(args[3], "dirty") == 0)
{
while (1)
{
FILE *meminfo = fopen("/proc/meminfo", "r");
ssize_t reads;
size_t len = 0;
char *line = NULL;
if (meminfo == NULL)
{
perror("Error opening meminfo file: ");
return;
}
int i = 0;
while (i < 17 && (reads = getline(&line, &len, meminfo)) != -1)
{
i++;
}
printf("%s", line);
fclose(meminfo);
timeout.tv_sec = frequency; // frequency seconds
timeout.tv_usec = 0; // 0 milliseconds
select(1, &input_set, NULL, NULL, &timeout);
}
}
free(line);
}