-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
178 lines (148 loc) · 6.1 KB
/
main.c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <signal.h>
#include <memory.h>
#include "sampling_gettimeofday.h"
#include "sample_predictive.h"
#include "stats.h"
unsigned int numberOfSamples, actualNumberOfSamples;
double totalSamplingTime, samplingPeriod;
static char type;
double *timestampsArray, *samplingPeriodsArray, *statsArray;
/// \brief Handler of SIGALRM signal. Used to terminate sampling when totalSamplingTime finishes.
/// \author Thanasis Charisoudis
///
/// \param signo
/// \return void - Actually this function terminates program execution.
static void onAlarm(int);
/// \brief Writes sampling output to 3 text files: 1) TimestampsArray.txt, 2) SamplingPeriodsArray.txt & 3) Stats.txt
/// \author Thanasis Charisoudis
///
/// \param timestampsArray - array holding all taken timestamps
/// \param samplingPeriodsArray - array holding all recorded periods ( between consecutive samples )
/// \param statsArray - array containing [min, max, mean, median, deviation]
/// \param numberOfSamples - number of samples in this sampling realization
/// \param samplingPeriod - time period between consecutive samples
/// \param type - defines the sampling method ( either gettimeofday() or clock_gettime() )
/// \return void
void toTxt(double *, double *, double *, unsigned int, double, char);
/// \brief Compare function used be qsort().
/// \param a - left element of comparison
/// \param b - right element of comparison
/// \return 1 if a > b, 0 if equal, -1 otherwise
static int compare(const void * a, const void * b);
int main(int argc, char **argv)
{
// Initial Data
char *ptr;
totalSamplingTime = strtod(argv[1], &ptr);
samplingPeriod = strtod(argv[2], &ptr);
numberOfSamples = (unsigned int) round(totalSamplingTime / samplingPeriod);
type = (char) ((argc < 4 ) ? 'c' : argv[3][0]);
// Allocate Memory
timestampsArray = (double *) malloc( numberOfSamples * sizeof( double ) );
samplingPeriodsArray = (double *) malloc( numberOfSamples * sizeof( double ) );
statsArray = (double *) malloc( 5 * sizeof( double ) );
// Setup alarm
alarm((unsigned int) totalSamplingTime);
signal(SIGALRM, onAlarm);
// initial value of actualNumberOfSamples
actualNumberOfSamples = 0;
// Sample using selected type
if ( type == 'd' )
{
sample_gettimeofday(timestampsArray, &actualNumberOfSamples);
}
else
{
// start predictor thread
if ( pthread_create( &predictor_thread, NULL, (void *) predictor_work, NULL) )
{
fprintf( stderr,"Error in pthread_create()! Exiting...\n" );
exit(EXIT_FAILURE);
}
// start sampling on main thread
sample_predictive(timestampsArray, &actualNumberOfSamples);
}
return 0;
}
static int compare(const void * a, const void * b)
{
double ad, bd;
ad = *(double*)a;
bd = *(double*)b;
if ( ad == bd ) return 0;
return ad > bd ? 1 : -1;
}
void toTxt(double* timestampsArray, double* samplingPeriodsArray, double* statsArray, unsigned int n,
double samplingPeriod, char type)
{
// 1) Write timestamps
FILE* fp = fopen("TimestampsArray.txt", "a"); // open in append mode
fprintf(fp, "--- New Sampling ( %d/%d samples - %.4f secs period - using %s ) ---\n",
n + 1, numberOfSamples, samplingPeriod, type == 'c' ? "clock_gettime()" : "gettimeofday()");
int i = 0;
do
{
fprintf(fp, "%.10f\n", timestampsArray[i]);
}
while( ++i < ( n + 1 ) );
fclose(fp);
// 2) Write periods
fp = fopen("SamplingPeriodsArray.txt", "a"); // open in append mode
fprintf(fp, "--- New Sampling ( %d/%d samples - %.4f secs period - using %s ) ---\n",
n, numberOfSamples - 1, samplingPeriod, type == 'c' ? "clock_gettime()" : "gettimeofday()");
i = 0;
do
{
fprintf(fp, "%.10f\n", samplingPeriodsArray[i]);
}
while( ++i < n );
fclose(fp);
// 3) Write stats
fp = fopen("Stats.txt", "a");
fprintf(fp, "--- New Sampling ( %d/%d samples - %.4f secs period - using %s ) ---\n",
n, numberOfSamples, samplingPeriod, type == 'c' ? "clock_gettime()" : "gettimeofday()");
fprintf(fp, "min: %.10f \n", statsArray[0]);
fprintf(fp, "max: %.10f \n", statsArray[1]);
fprintf(fp, "mean: %.10f \n", statsArray[2]);
fprintf(fp, "median: %.10f \n", statsArray[3]);
fprintf(fp, "deviation: %.10f \n", statsArray[4]);
fclose(fp);
printf("\t--> results written to respective text files\n");
}
static void onAlarm(int signo)
{
fprintf(stdout, "Caught the SIGALRM signal ( signo = %d )\n", signo);
// join pthread
pthread_cancel(predictor_thread );
// Calculate periods
int i = 0;
do
{
samplingPeriodsArray[i] = timestampsArray[i+1] - timestampsArray[i];
}
while( ++i < actualNumberOfSamples - 1 );
// Sort periods in ascending order ( periods are one less than samples )
actualNumberOfSamples -= 1;
// Sort array ( retaining old )
double *samplingPeriodsArraySorted;
samplingPeriodsArraySorted = malloc( actualNumberOfSamples * sizeof( double ) );
memcpy( samplingPeriodsArraySorted, samplingPeriodsArray, actualNumberOfSamples * sizeof( double ) );
qsort( samplingPeriodsArraySorted, actualNumberOfSamples, sizeof(double), compare );
// Stats calculation
statsArray[0] = stats_min(samplingPeriodsArraySorted, actualNumberOfSamples);
statsArray[1] = stats_max(samplingPeriodsArraySorted, actualNumberOfSamples);
statsArray[2] = stats_mean(samplingPeriodsArraySorted, actualNumberOfSamples);
statsArray[3] = stats_median(samplingPeriodsArraySorted, actualNumberOfSamples);
statsArray[4] = stats_deviation(samplingPeriodsArraySorted, actualNumberOfSamples, statsArray[2]);
free( samplingPeriodsArraySorted );
// Write to txt
toTxt(timestampsArray, samplingPeriodsArray, statsArray, actualNumberOfSamples, samplingPeriod, type);
// Free Memory
free( timestampsArray );
free( samplingPeriodsArray );
free( statsArray );
printf("Exiting now...\n");
exit(EXIT_SUCCESS);
}
// terminal: cd cmake-build-debug && ./project1 7200 0.1 c && ./project1 7200 0.1 d