forked from joshilp/Bounded-Buffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.c
More file actions
154 lines (126 loc) · 3.86 KB
/
stats.c
File metadata and controls
154 lines (126 loc) · 3.86 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
146
147
148
149
150
151
152
153
154
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "stats.h"
#include <semaphore.h>
sem_t mutex;
int producers = 0;
typedef struct factorystats
{
// keep track of factory number
int factory_number;
// 1. Count the number of candies each factory creates. Called from the candy-factory thread.
int candies_produced;
// 2. Count the number of candies that were consumed from each factory.
int candies_consumed;
// 3. For each factory, the min, max, and average delays for how long it took from the moment the
// candy was produced (dynamically allocated) until consumed (eaten by the kid). This will be
// done by the factory thread calling the stats code when a candy is created, and the kid thread
// calling the stats code when an item is consumed.
double min_delay;
double max_delay;
double average_delay;
double total_delay;
} factorystats_t;
//initialize factories
factorystats_t* factories;
//initializes factorystats variables
void stats_init(int num_producers)
{
sem_init(&mutex, 0, 1);
// set variable for producers
producers = num_producers;
// array to track each strat struct
factories = malloc(sizeof(factorystats_t) * num_producers);
for (int i = 0; i < num_producers; i++)
{
factories[i].factory_number = i;
factories[i].candies_produced = 0;
factories[i].candies_consumed = 0;
factories[i].min_delay = -1;
factories[i].max_delay = -1;
factories[i].average_delay = -1;
factories[i].total_delay = 0;
}
}
//free factories
void stats_cleanup(void)
{
free(factories);
factories = NULL;
}
//records candy production stats
void stats_record_produced(int factory_number)
{
//waits
sem_wait(&mutex);
//increase number produced
factories[factory_number].candies_produced++;
//signals
sem_post(&mutex);
}
//record candy consumption stats
void stats_record_consumed(int factory_number, double delay_in_ms)
{
//waits
//locks so can change data without other process affecting stats
sem_wait(&mutex);
//increments candies consumed
factories[factory_number].candies_consumed++;
//sets variables to consumed, min delay, max delay, and total delay to be used
//for calculations
// int facnum = factories[i].factory_number;
// int c_prod = factories[i].candies_produced;
double c_cons = (double)factories[factory_number].candies_consumed;
double mind = factories[factory_number].min_delay;
double maxd = factories[factory_number].max_delay;
double totald = factories[factory_number].total_delay;
//check if still at default value
if (mind == -1)
{
factories[factory_number].min_delay = delay_in_ms;
factories[factory_number].max_delay = delay_in_ms;
factories[factory_number].average_delay = delay_in_ms;
factories[factory_number].total_delay = delay_in_ms;
}
//sets new min and max values
else
{
// new min
if (delay_in_ms < mind)
{
factories[factory_number].min_delay = delay_in_ms;
}
//new max
if (delay_in_ms > maxd)
{
factories[factory_number].max_delay = delay_in_ms;
}
//calculate average using total delay divided by amount consumed
factories[factory_number].total_delay += delay_in_ms;
factories[factory_number].average_delay = (totald/c_cons);
}
//signals
sem_post(&mutex);
}
//prints out statistics
void stats_display(void)
{
printf("\nStatistics:\n\n");
printf("%s%8s%8s%16s%16s%16s\n", "Factory#", "#Made", "#Eaten", "Min Delay[ms]", "Avg Delay[ms]", "Max Delay[ms]");
//displays all stats in struct
for (int i = 0; i < producers; i++)
{
int facnum = factories[i].factory_number;
int c_prod = factories[i].candies_produced;
int c_cons = factories[i].candies_consumed;
double mind = factories[i].min_delay;
double avgd = factories[i].average_delay;
double maxd = factories[i].max_delay;
printf("%4d%10d%8d%16f%16f%16f\n", facnum, c_prod, c_cons, mind, avgd, maxd);
if (c_prod != c_cons)
{
printf("ERROR: Mismatch between number made and eaten.\n");
}
}
}