-
Notifications
You must be signed in to change notification settings - Fork 2
/
pthread_test_create.c
136 lines (104 loc) · 2.68 KB
/
pthread_test_create.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
#define _GNU_SOURCE
#include<stdio.h>
#include<fcntl.h>
#include<time.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdint.h>
#include<stdbool.h>
#include<pthread.h>
#include<sys/mman.h>
#include<sys/time.h>
char *dir = "/mnt/ramdisk/file_";
int num_threads;
int num_files;
//Doorbell. Each 64 bytes long to avoid cache contention.
volatile uint64_t doorbell[8 * 16];
#define START 0x1
#define FINISHED 0x2
void start_all_pthreads(void)
{
int i;
for (i = 0; i < num_threads; i++)
doorbell[i * 8] = START;
}
bool all_pthreads_finished(void)
{
int i;
for (i = 0; i < num_threads; i++)
if (doorbell[i * 8] != FINISHED)
return false;
return true;
}
inline bool pthread_can_start(int pid)
{
return (doorbell[pid * 8] == START);
}
inline void signal_pthread_finished(int pid)
{
doorbell[pid * 8] = FINISHED;
}
void* pthread_file_create(void *arg)
{
struct timespec start, end;
long long time;
char filename[60];
// char command[120];
int pid = *(int *)arg;
int fd;
int file_count;
// printf("start pthread: %d\n", pid);
while (!pthread_can_start(pid))
;
clock_gettime(CLOCK_MONOTONIC, &start);
for (file_count = 0; file_count < num_files; file_count++) {
sprintf(filename, "%s%d%d", dir, pid, file_count);
// sprintf(command, "%s %s", "touch", filename);
// system(command);
fd = open(filename, O_CREAT | O_RDWR, 0640);
close(fd);
}
clock_gettime(CLOCK_MONOTONIC, &end);
time = (end.tv_sec - start.tv_sec) * 1e9 + (end.tv_nsec - start.tv_nsec);
printf("Thread %d: %lld nanoseconds, each %lld ns\n", pid, time, time / num_files);
signal_pthread_finished(pid);
pthread_exit(0);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t *pthreads;
int pids[16];
int i;
long long time;
struct timespec start, end;
if (argc < 3) {
printf("Usage: ./pthread_test_create $NUM_FILE $NUM_THREADS\n");
return 0;
}
num_files = atoi(argv[1]);
num_threads = atoi(argv[2]);
if (num_threads <= 0 || num_threads > 16)
num_threads = 1;
printf("%d files, %d pthreads\n", num_files, num_threads);
system("rm -rf /mnt/ramdisk/*");
//Allocate the threads
pthreads = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
for (i = 0; i < num_threads; i++) {
pids[i] = i;
pthread_create(pthreads + i, NULL, pthread_file_create, (void *)(pids + i));
}
clock_gettime(CLOCK_MONOTONIC, &start);
start_all_pthreads();
while (!all_pthreads_finished())
;
clock_gettime(CLOCK_MONOTONIC, &end);
time = (end.tv_sec - start.tv_sec) * 1e9 + (end.tv_nsec - start.tv_nsec);
printf("%lld nanoseconds, each %lld ns\n", time, time / (num_files * num_threads));
for (i = 0; i < num_threads; i++) {
pthread_join(pthreads[i], NULL);
}
free(pthreads);
return 0;
}