Skip to content

Commit 334d1f6

Browse files
committed
Change MakeFile
Add header files Add main file
1 parent 6d38eea commit 334d1f6

File tree

6 files changed

+74
-30
lines changed

6 files changed

+74
-30
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ a.out
44
.vscode/settings.json
55
reader
66
reader.o
7+
cputracker.o
8+
cputracker

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
CC = gcc -g
22
CFLAGS = -O3 -Wall -Werror
33

4-
OBJS = reader.o
4+
OBJS = reader.o cputracker.o
55

66
reader: $(OBJS)
7-
$(CC) $(CFLAGS) -o reader $(OBJS)
7+
$(CC) $(CFLAGS) -o cputracker $(OBJS)
88

9-
reader.o: reader.c reader.h
9+
reader.o: reader.c reader.h cputracker.h
10+
cputracker.o: cputracker.c reader.h cputracker.h
1011

1112
format:
1213
clang-format --style=file -i *.c *.h

cputracker.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "cputracker.h"
2+
#include "reader.h"
3+
4+
int nproc = 0;
5+
6+
int main(){
7+
nproc = get_nproc() + 1;
8+
9+
print_stats();
10+
return 0;
11+
}

cputracker.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef CPUTRACKER_H
2+
#define CPUTRACKER_H
3+
4+
#include <assert.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
9+
#define EXIT_FAILURE 1
10+
#define EXIT_SUCCESS 0
11+
12+
13+
extern int nproc;
14+
15+
struct proc_stat {
16+
char name[256];
17+
unsigned long user, // Time spent with normal processing in user mode.
18+
nice, // Time spent with niced processes in user mode.
19+
system, // Time spent running in kernel mode.
20+
idle, // Time spent in vacations twiddling thumbs.
21+
iowait, // Time spent waiting for I/O to completed. This is considered
22+
// idle time too.
23+
irq, // Time spent serving hardware interrupts.
24+
softirq, // Time spent serving software interrupts.
25+
steal, // Time stolen by other operating systems running in a virtual
26+
// environment.
27+
guest, // Time spent for running a virtual CPU or guest OS under the
28+
// control of the kernel.
29+
guest_nice; // Time spent running a niced guest (virtual CPU for guest
30+
// operating systems under the control of the Linux kernel).
31+
};
32+
33+
#endif

reader.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include "reader.h"
2-
#include <stdio.h>
3-
#include <stdlib.h>
4-
#include <string.h>
5-
#include <assert.h>
2+
#include "cputracker.h"
63

74
int get_nproc() {
85
FILE *fp;
@@ -11,7 +8,7 @@ int get_nproc() {
118
fp = popen("nproc", "r");
129
if (fp == NULL) {
1310
printf("Reading number of threads failed\n");
14-
exit(1);
11+
exit(EXIT_FAILURE);
1512
}
1613

1714
int nproc = 0;
@@ -23,21 +20,29 @@ int get_nproc() {
2320
return nproc;
2421
}
2522

26-
int main() {
23+
struct proc_stat *get_proc_stats() {
2724
FILE *file = fopen("/proc/stat", "r");
2825
char line[1024];
29-
int nproc = get_nproc() + 1;
30-
struct proc_stat stats[nproc];
26+
struct proc_stat *stats = malloc(nproc * sizeof(struct proc_stat));
3127

3228
for (int thread = 0; thread < nproc; thread++) {
3329
fgets(line, sizeof(line), file);
34-
assert(strncmp(line, "cpu", 3) == 0);
30+
// assert(strncmp(line, "cpu", 3) == 0);
31+
if (strncmp(line, "cpu", 3) != 0) {
32+
printf("Reading thread info failed\n");
33+
exit(EXIT_FAILURE);
34+
}
3535
sscanf(line, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
3636
stats[thread].name, &stats[thread].user, &stats[thread].nice,
3737
&stats[thread].system, &stats[thread].idle, &stats[thread].iowait,
3838
&stats[thread].irq, &stats[thread].softirq, &stats[thread].steal,
3939
&stats[thread].guest, &stats[thread].guest_nice);
4040
}
41+
return stats;
42+
}
43+
44+
int print_stats() {
45+
struct proc_stat *stats = get_proc_stats();
4146

4247
for (int i = 0; i < nproc; i++) {
4348
printf("%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", stats[i].name,
@@ -46,6 +51,6 @@ int main() {
4651
stats[i].guest, stats[i].guest_nice);
4752
}
4853

49-
fclose(file);
54+
free(stats);
5055
return 0;
5156
}

reader.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
struct proc_stat {
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).
17-
};
1+
#ifndef READER_H
2+
#define READER_H
3+
4+
5+
int print_stats();
6+
struct proc_stat *get_proc_stats();
7+
int get_nproc();
8+
9+
#endif

0 commit comments

Comments
 (0)