forked from usagainsttheworld/GetFileClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkload.c
More file actions
53 lines (37 loc) · 1.14 KB
/
workload.c
File metadata and controls
53 lines (37 loc) · 1.14 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "workload.h"
static char *gWorkloadPathArray[100];
static unsigned short int gUniqueWorkloadPaths = 0;
static pthread_mutex_t counter_mutex;
static int counter = 0;
static int mode = WORKLOAD_SEQ;
int workload_init(char *workload_path) {
int i = 0;
char temp_buf[256];
FILE *file_handle;
file_handle = fopen(workload_path, "r");
if (file_handle == NULL) {
fprintf(stderr, "cannot open workload file %s", workload_path);
return EXIT_FAILURE;
}
while (fscanf(file_handle, "%s", temp_buf) != EOF)
gWorkloadPathArray[i++] = strdup(temp_buf);
gUniqueWorkloadPaths = i;
pthread_mutex_init(&counter_mutex, NULL);
fclose(file_handle);
return EXIT_SUCCESS;
}
unsigned short int workload_num_unique_paths(){
return gUniqueWorkloadPaths;
}
char* workload_get_path(){
if(mode == WORKLOAD_RND)
return gWorkloadPathArray[(int)(1.0 * gUniqueWorkloadPaths * rand()/(RAND_MAX+1.0))];
pthread_mutex_lock(&counter_mutex);
counter++;
pthread_mutex_unlock(&counter_mutex);
return gWorkloadPathArray[counter % gUniqueWorkloadPaths];
}