-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.c
More file actions
336 lines (285 loc) · 9.51 KB
/
reader.c
File metadata and controls
336 lines (285 loc) · 9.51 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/wait.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image/stb_image_write.h"
#define MAX_IMAGES 50
// STRUCT
// struct for the shared memory blocks
typedef struct
{
unsigned char value;
int id;
time_t raw_pixel_time;
sem_t sem_write_resource;
} QueueData;
// struct for the Queue data
typedef struct
{
sem_t sem_write;
sem_t sem_read;
sem_t sem_filled;
sem_t sem_empty;
int next_input;
int next_output;
int chunk_size;
} QueueInfo;
// struct for the extra information of each image
typedef struct
{
int id;
int width;
int height;
int channels;
} ImageData;
// struct with the stats
typedef struct
{
unsigned long real_memory_used;
unsigned long virtual_memory_used;
double blocked_sem_time;
int total_pixels_processed;
double total_kernel_time;
int pixels_greater_than_175;
int encoders_counter;
int decoders_counter;
int total_enco;
int total_deco;
} Stats;
/**
* This function passes an int into a binary for the key
*/
int getDecimal(int clave)
{
int num = clave;
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
/**
* This method builds the image based in the data array
*/
void buildImage(int id, int width, int height, int channels, unsigned char *pixels)
{
printf("Creating the image\n");
// contruct filename
char id_str[10];
sprintf(id_str, "%d", id);
char filename[] = "result_";
strcat(filename, id_str);
strcat(filename, ".jpg");
// save image
stbi_write_jpg(filename, width, height, channels, pixels, width*channels);
printf("Image saved as %s\n", filename);
}
/**
* Read the information from the shared memory
*/
void read_info(QueueData *queue, QueueInfo *queue_info, Stats *stats, ImageData *images, char* mode, int* key, int id)
{
stats->total_deco++;
// Get image dimensions and data for the analysis
int width, height, channels;
width = images[id].width;
height = images[id].height;
channels = images[id].channels;
unsigned char img2[width * height*channels];
int clave = key;
// Iterate for each image pixel
for (int i = 0; i < width * height * channels; )
{
// count wait time and initialize semaphores
clock_t begin_sem = clock();
sem_wait(&queue_info->sem_filled);
sem_wait(&queue_info->sem_read);
clock_t end_sem = clock();
int index = queue_info->next_output;
if (queue[index].id == id)
{
// read the value stored in shared memory
clock_t begin = clock();
unsigned char val = queue[index].value;
printf("Reading value: %d in position: %d from id: %d\n", val, i, queue[index].id);
int timeInfo = getTime(queue->raw_pixel_time);
val = val ^ getDecimal(clave);
img2[i] = val;
clock_t end = clock();
// Adding reading time
stats->total_kernel_time += (double)(end - begin) / CLOCKS_PER_SEC; // Adding kernel time to stats
stats->blocked_sem_time += (double)(end_sem - begin_sem) / CLOCKS_PER_SEC; // Adding blocked sem time to stats
// wait for an enter hit when mode is manual
if (strcmp(mode, "manual") == 0)
{
char received_char = getchar();
if (received_char == 'q') mode = "auto";
}
queue_info->next_output = (i+1) % queue_info->chunk_size; // for circular list
i++;
sem_post(&queue_info->sem_read);
sem_post(&queue_info->sem_empty);
}
else
{
// in case the value is not from the image do ups in the semaphores
sem_post(&queue_info->sem_read);
sem_post(&queue_info->sem_filled);
}
}
// build the image from the int array
buildImage(id, width, height, channels, img2);
}
/*
* Measures the current (and peak) resident and virtual memory
* usage of your linux C process, in bytes, accurate to nearest kB.
* Returns a 0 if memory info access was successful, else prints
* an error message and returns 1
*/
int getMemory( unsigned long *currRealMem, unsigned long *peakRealMem, unsigned long *currVirtMem, unsigned long *peakVirtMem, Stats *stats) {
// stores each word in status file
char buffer[1024] = "";
// linux file contains this-process info
FILE* file = NULL;
file = fopen("/proc/self/status", "r");
if (file == NULL) {
printf("Call to getMemory FAILED; "
"linux file proc/self/status not found!\n");
return 1;
}
// read the entire file, recording mems in kB
while (fscanf(file, " %1023s", buffer) == 1) {
if (strcmp(buffer, "VmRSS:") == 0) {
fscanf(file, " %lu", currRealMem);
}
if (strcmp(buffer, "VmHWM:") == 0) {
fscanf(file, " %lu", peakRealMem);
}
if (strcmp(buffer, "VmSize:") == 0) {
fscanf(file, " %lu", currVirtMem);
}
if (strcmp(buffer, "VmPeak:") == 0) {
fscanf(file, " %lu", peakVirtMem);
}
}
fclose(file);
// convert kB to bytes
unsigned int factor = 1000;
*currRealMem *= factor;
stats->real_memory_used += *currRealMem;
*peakRealMem *= factor;
*currVirtMem *= factor;
stats->virtual_memory_used += *currVirtMem;
*peakVirtMem *= factor;
}
/**
* Get the time information
*/
int getTime(time_t rawtime){
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Pixel saved local time and date: %s\n", asctime (timeinfo) );
}
int main(int argc, char *argv[])
{
// Get arguments from command line
char mode[50];
strcpy(mode, "");
int key = -1;
// Se leen los argumentos
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-m") == 0) {
strcpy(mode, argv[i + 1]);
}
if (strcmp(argv[i], "-k") == 0) {
key = atoi(argv[i + 1]);
}
}
// validate the arguments
if (strcmp(mode, "") == 0 || key < 1) {
printf("Can not identify the key\n");
return 1;
}
// opens the file descriptor that has to be mapped to the
// shared memory
int fd_info = open("/tmp/project_1_info", O_RDWR | O_CREAT, 0644);
if (fd_info < 0) {
perror("project_1_info");
exit(1);
}
ftruncate(fd_info, sizeof(QueueInfo));
// file descriptor for the stats
int fd_stats = open("/tmp/project_1_stats", O_RDWR | O_CREAT, 0644);
if (fd_stats < 0) {
perror("project_1_stats");
exit(1);
}
ftruncate(fd_stats, sizeof(Stats));
// file descriptor for the images file
int fd_images = open("/tmp/project_1_images", O_RDWR | O_CREAT, 0644);
if (fd_stats < 0)
{
perror("project_1_images");
exit(1);
}
ftruncate(fd_images, MAX_IMAGES * sizeof(ImageData));
// memory mapping function instance
// address: NULL means the kernel can place the mapping anywhere it sees fit
// lenght: number of bytes to be mapped, the array will contain 10 integers
// protect: PROT_READ | PROT_WRITE the access allows reading and writing on the content
// flags: MAP_SHARED this flag is used to share the mapping with all other processes,
// which are mapped to this object
// filedes: file descriptor to be mapped
// offset: 0, offset from where the mapping started
// source: https://linuxhint.com/using_mmap_function_linux/
QueueInfo *queue_info = mmap(NULL, sizeof(QueueInfo), PROT_READ | PROT_WRITE,
MAP_SHARED, fd_info, 0);
Stats *stats = mmap(NULL, sizeof(Stats), PROT_READ | PROT_WRITE,
MAP_SHARED, fd_stats, 0);
ImageData *images = mmap(NULL, MAX_IMAGES*sizeof(ImageData), PROT_READ | PROT_WRITE,
MAP_SHARED, fd_images, 0);
// file descriptor for the queue
int fd_queue = open("/tmp/project_1_queue", O_RDWR | O_CREAT, 0644);
if (fd_queue < 0) {
perror("project_1_queue");
exit(1);
}
ftruncate(fd_queue, queue_info->chunk_size *sizeof(QueueData));
QueueData *queue = mmap(NULL, queue_info->chunk_size *sizeof(QueueData), PROT_READ | PROT_WRITE,
MAP_SHARED, fd_queue, 0);
stats->decoders_counter += 1;
// fill the queue with the data
read_info(queue, queue_info, stats, images, mode, key, stats->total_deco);
// finish the file descriptors that are no longer needed
unlink("/tmp/project_1_queue");
unlink("/tmp/project_1_info");
stats->decoders_counter -= 1;
unsigned long currRealMem, peakRealMem, currVirtMem, peakVirtMem;
int success = getMemory(&currRealMem, &peakRealMem, &currVirtMem, &peakVirtMem, stats);
// if there are no other processes alive show the stats
if (stats->encoders_counter == 0 && stats->decoders_counter == 0){
int callStats = system("./stats");
}
unlink("/tmp/project_1_stats");
close(fd_queue);
close(fd_info);
close(fd_stats);
return 0;
}