Skip to content

Commit a4eabb2

Browse files
committed
Add read delay and function to calculate average_cpu_usage
1 parent f97342a commit a4eabb2

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

analyzer.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
11
#include "analyzer.h"
22
#include "cputracker.h"
33

4+
5+
unsigned long average_cpu_usage(struct proc_stat prev, struct proc_stat next){
6+
unsigned long prev_idle = prev.idle + prev.iowait;
7+
unsigned long idle = next.idle + next.iowait;
8+
9+
unsigned long prev_non_idle = prev.user + prev.nice + prev.system + prev.irq + prev.softirq + prev.steal;
10+
unsigned long non_idle = next.user + next.nice + next.system + next.irq + next.softirq + next.steal;
11+
12+
unsigned long prev_total = prev_idle + prev_non_idle;
13+
unsigned long total = idle + non_idle;
14+
15+
total -= prev_total;
16+
idle -= prev_idle;
17+
18+
return (total-idle)*100/total;
19+
}
20+
21+
422
void *analyzer() {
23+
struct proc_stat *prev = malloc(g_nproc*sizeof(struct proc_stat));
24+
struct proc_stat *stats = NULL;
525
while (1) {
626
sem_wait(&g_filledSpaceSemaphore);
27+
728
pthread_mutex_lock(&g_bufferMutex);
829

9-
struct proc_stat *stats = get_item();
30+
stats = get_item_from_buffer();
31+
for(int i = 0; i < g_nproc; i++){
32+
printf("%lu ", average_cpu_usage(prev[i], stats[i]));
33+
prev[i] = stats[i];
34+
}
1035

36+
printf("\n");
1137
pthread_mutex_unlock(&g_bufferMutex);
12-
sem_post(&g_leftSpaceSemaphore);
1338

14-
for (int i = 0; i < g_nproc; i++) {
15-
printf("%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", stats[i].name,
16-
stats[i].user, stats[i].nice, stats[i].system, stats[i].idle,
17-
stats[i].iowait, stats[i].irq, stats[i].softirq, stats[i].steal,
18-
stats[i].guest, stats[i].guest_nice);
19-
}
39+
sem_post(&g_leftSpaceSemaphore);
2040
}
2141
}

analyzer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
#include "cputracker.h"
44

55
void *analyzer();
6+
unsigned long average_cpu_usage(struct proc_stat prev, struct proc_stat next);
67
#endif

cputracker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int get_semaphore_value(sem_t *sem) {
2222
return sval;
2323
}
2424

25-
struct proc_stat *get_item() {
25+
struct proc_stat *get_item_from_buffer() {
2626
int index = get_semaphore_value(&g_filledSpaceSemaphore);
2727
return g_buffer[index];
2828
}

cputracker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ extern sem_t g_leftSpaceSemaphore;
4141

4242
int get_nproc(int *nproc);
4343
int get_semaphore_value(sem_t *sem);
44-
struct proc_stat *get_item();
44+
struct proc_stat *get_item_from_buffer();
4545
#endif

reader.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ int get_proc_stats(struct proc_stat *stats) {
2222
}
2323

2424
void *reader() {
25+
struct proc_stat *stats = NULL;
2526
while (1) {
2627

2728
sem_wait(&g_leftSpaceSemaphore);
2829

2930
pthread_mutex_lock(&g_bufferMutex);
3031

31-
struct proc_stat *stats = get_item();
32+
stats = get_item_from_buffer();
33+
3234
if (get_proc_stats(stats) == -1) {
3335
pthread_mutex_unlock(&g_bufferMutex);
3436
continue;
@@ -37,5 +39,6 @@ void *reader() {
3739
pthread_mutex_unlock(&g_bufferMutex);
3840

3941
sem_post(&g_filledSpaceSemaphore);
42+
usleep(READ_DELAY);
4043
}
4144
}

reader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define READER_H
33
#include "cputracker.h"
44

5+
#define READ_DELAY 30000
6+
57
int get_proc_stats(struct proc_stat *stats);
68
void *reader();
79

0 commit comments

Comments
 (0)