Skip to content

Commit 8d6c2db

Browse files
committed
Add simple Makefile
Add get_nproc func
1 parent 941a652 commit 8d6c2db

File tree

4 files changed

+74
-35
lines changed

4 files changed

+74
-35
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ a.out
22
.vscode/c_cpp_properties.json
33
.vscode/launch.json
44
.vscode/settings.json
5+
reader
6+
reader.o

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CC = gcc -g
2+
CFLAGS = -O3 -Wall -Werror
3+
4+
OBJS = reader.o
5+
6+
reader: $(OBJS)
7+
$(CC) $(CFLAGS) -o reader $(OBJS)
8+
9+
reader.o: reader.c reader.h
10+
11+
format:
12+
clang-format --style=file -i *.c *.h

reader.c

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,53 @@
1+
#include "reader.h"
12
#include <stdio.h>
2-
#include <string.h>
33
#include <stdlib.h>
4-
#include "reader.h"
4+
#include <string.h>
5+
6+
int get_nproc() {
7+
FILE *fp;
8+
char output[64];
9+
10+
fp = popen("nproc", "r");
11+
if (fp == NULL) {
12+
printf("Reading number of threads failed\n");
13+
exit(1);
14+
}
15+
16+
int nproc = 0;
17+
if (fgets(output, sizeof(output) - 1, fp) != NULL) {
18+
sscanf(output, "%d", &nproc);
19+
}
20+
21+
pclose(fp);
22+
return nproc;
23+
}
524

625
int main() {
7-
FILE* file = fopen("/proc/stat", "r");
8-
char line[1024];
9-
struct proc_stat* stats = NULL;
10-
int thread_count = 0;
11-
int capacity = 0;
12-
13-
while (fgets(line, sizeof(line), file)) {
14-
if (strncmp(line, "cpu", 3) == 0) {
15-
if (thread_count == capacity) {
16-
capacity = capacity == 0 ? 1 : capacity * 2;
17-
struct proc_stat* temp = realloc(stats, capacity * sizeof(struct proc_stat));
18-
if(temp == NULL) {
19-
perror("Error reallocating memory");
20-
free(stats);
21-
fclose(file);
22-
return 1;
23-
}
24-
stats = temp;
25-
}
26-
sscanf(line, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", stats[thread_count].name, &stats[thread_count].user, &stats[thread_count].nice, &stats[thread_count].system, &stats[thread_count].idle, &stats[thread_count].iowait, &stats[thread_count].irq, &stats[thread_count].softirq, &stats[thread_count].steal, &stats[thread_count].guest, &stats[thread_count].guest_nice);
27-
thread_count++;
28-
}else{
29-
break;
30-
}
31-
}
26+
FILE *file = fopen("/proc/stat", "r");
27+
char line[1024];
28+
struct proc_stat stats[get_nproc() + 1];
29+
int thread_count = 0;
3230

33-
for (int i = 0; i < thread_count; i++) {
34-
printf("%s %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", stats[i].name, stats[i].user, stats[i].nice, stats[i].system, stats[i].idle, stats[i].iowait, stats[i].irq, stats[i].softirq, stats[i].steal, stats[i].guest, stats[i].guest_nice);
31+
while (fgets(line, sizeof(line), file)) {
32+
if (strncmp(line, "cpu", 3) == 0) {
33+
sscanf(line, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
34+
stats[thread_count].name, &stats[thread_count].user,
35+
&stats[thread_count].nice, &stats[thread_count].system,
36+
&stats[thread_count].idle, &stats[thread_count].iowait,
37+
&stats[thread_count].irq, &stats[thread_count].softirq,
38+
&stats[thread_count].steal, &stats[thread_count].guest,
39+
&stats[thread_count].guest_nice);
40+
thread_count++;
3541
}
42+
}
43+
44+
for (int i = 0; i < thread_count; i++) {
45+
printf("%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", stats[i].name,
46+
stats[i].user, stats[i].nice, stats[i].system, stats[i].idle,
47+
stats[i].iowait, stats[i].irq, stats[i].softirq, stats[i].steal,
48+
stats[i].guest, stats[i].guest_nice);
49+
}
3650

37-
free(stats);
38-
fclose(file);
39-
return 0;
51+
fclose(file);
52+
return 0;
4053
}

reader.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
21
struct proc_stat {
3-
char name[256];
4-
unsigned long user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
2+
char name[256];
3+
unsigned long user, // Time spent with normal processing in user mode.
4+
nice, // Time spent with niced processes in user mode.
5+
system, // Time spent running in kernel mode.
6+
idle, // Time spent in vacations twiddling thumbs.
7+
iowait, // Time spent waiting for I/O to completed. This is considered
8+
// idle time too.
9+
irq, // Time spent serving hardware interrupts.
10+
softirq, // Time spent serving software interrupts.
11+
steal, // Time stolen by other operating systems running in a virtual
12+
// environment.
13+
guest, // Time spent for running a virtual CPU or guest OS under the
14+
// control of the kernel.
15+
guest_nice; // Time spent running a niced guest (virtual CPU for guest
16+
// operating systems under the control of the Linux kernel).
517
};

0 commit comments

Comments
 (0)