Skip to content

Commit e40e888

Browse files
committed
Change buffer to a fixed size
1 parent dc650a8 commit e40e888

File tree

6 files changed

+24
-23
lines changed

6 files changed

+24
-23
lines changed

analyzer.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#include "analyzer.h"
2-
#include "cputracker.h"
3-
#include <stdio.h>
4-
int x = 0;
52

63
void *analyzer() {
74
while (1) {
@@ -19,6 +16,5 @@ void *analyzer() {
1916
stats[i].iowait, stats[i].irq, stats[i].softirq, stats[i].steal,
2017
stats[i].guest, stats[i].guest_nice);
2118
}
22-
free(stats);
2319
}
2420
}

analyzer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef ANALYZER_H
22
#define ANALYZER_H
3+
#include "cputracker.h"
34

45
void *analyzer();
56
#endif

cputracker.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ int get_semaphore_value(sem_t *sem) {
2323
return sval;
2424
}
2525

26-
int put_item(struct proc_stat *stats) {
26+
struct proc_stat *get_item() {
2727
int index = get_semaphore_value(&g_filledSpaceSemaphore);
2828
if (index > BUFFER_SIZE) {
29-
return -1;
29+
return NULL;
3030
}
31-
g_buffer[index] = stats;
32-
return 0;
31+
return g_buffer[index];
3332
}
3433

3534
struct proc_stat *remove_item() {
@@ -45,6 +44,12 @@ int main() {
4544
if (get_nproc(&g_nproc) == -1) {
4645
exit(EXIT_FAILURE);
4746
}
47+
for(int i = 0 ; i < BUFFER_SIZE; i++){
48+
g_buffer[i] = malloc(g_nproc * sizeof(struct proc_stat));
49+
if(g_buffer[i] == NULL){
50+
exit(EXIT_FAILURE);
51+
}
52+
}
4853

4954
if (sem_init(&g_filledSpaceSemaphore, 0, 0) ||
5055
sem_init(&g_leftSpaceSemaphore, 0, BUFFER_SIZE)) {

cputracker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ extern pthread_mutex_t g_bufferMutex;
3939
extern sem_t g_filledSpaceSemaphore;
4040
extern sem_t g_leftSpaceSemaphore;
4141

42-
int get_nproc();
42+
int get_nproc(int *nproc);
4343
int get_semaphore_value(sem_t *sem);
44-
int put_item(struct proc_stat *stats);
44+
struct proc_stat *get_item();
4545
struct proc_stat *remove_item();
4646
#endif

reader.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
#include "reader.h"
2-
#include "cputracker.h"
32

4-
struct proc_stat *get_proc_stats() {
3+
int get_proc_stats(struct proc_stat *stats) {
54
FILE *file = fopen("/proc/stat", "r");
65
char line[1024];
7-
struct proc_stat *stats = malloc(g_nproc * sizeof(struct proc_stat));
86
for (int thread = 0; thread < g_nproc; thread++) {
97
fgets(line, sizeof(line), file);
10-
// assert(strncmp(line, "cpu", 3) == 0);
118
if (strncmp(line, "cpu", 3) != 0) {
129
perror("Reading thread info failed");
1310
fclose(file);
14-
free(stats);
15-
return NULL;
11+
return -1;
1612
}
1713

1814
sscanf(line, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
@@ -22,21 +18,23 @@ struct proc_stat *get_proc_stats() {
2218
&stats[thread].guest, &stats[thread].guest_nice);
2319
}
2420
fclose(file);
25-
return stats;
21+
return 0;
2622
}
2723

2824
void *reader() {
29-
struct proc_stat *stats = NULL;
3025
while (1) {
31-
if ((stats = get_proc_stats()) == NULL) {
32-
return NULL;
33-
}
3426

3527
sem_wait(&g_leftSpaceSemaphore);
3628

3729
pthread_mutex_lock(&g_bufferMutex);
3830

39-
put_item(stats);
31+
struct proc_stat *stats = get_item();
32+
if(stats == NULL){
33+
continue;
34+
}
35+
if (get_proc_stats(stats) == -1) {
36+
continue;
37+
}
4038

4139
pthread_mutex_unlock(&g_bufferMutex);
4240

reader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#ifndef READER_H
22
#define READER_H
3+
#include "cputracker.h"
34

5+
int get_proc_stats(struct proc_stat *stats);
46
void *reader();
5-
struct proc_stat *get_proc_stats();
67

78
#endif

0 commit comments

Comments
 (0)