master_fromkland_same_line.c 3.36 KB
Newer Older
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
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>


struct ipc_message{
	char message[60];
	volatile uint32_t monitor;
}__attribute__((packed));

int shmid;
void *s_area = NULL;
const unsigned int S_SIZE = 4096 * 100;
const unsigned long NUM_LOOPS = 3136;

static unsigned long RDTSCP(void)
{
	unsigned long tsc;
	__asm__ __volatile__(
        "rdtscp;"
        "shl $32, %%rdx;"
        "or %%rdx, %%rax"
        : "=a"(tsc)
        :
        : "%rcx", "%rdx");

	return tsc;
}


static unsigned long RDTSC_START(void)
{

        unsigned cycles_low, cycles_high;

        asm volatile ("CPUID\n\t"
                      "RDTSC\n\t"
                      "mov %%edx, %0\n\t"
                      "mov %%eax, %1\n\t": "=r" (cycles_high), "=r" (cycles_low)::
                      "%rax", "%rbx", "%rcx", "%rdx");
        return ((unsigned long) cycles_high << 32) | cycles_low;

}



int initialize_shm() {

	key_t key = 12345;

	shmid = shmget(key, S_SIZE, 0666);
	   printf("shmid in master is %d\n",shmid);
	if (-1 == shmid) {
		printf("In master, there was no shm by that key, creating\n");
           if (ENOENT != errno) {
               return -2;
           }

           shmid = shmget(key, S_SIZE, IPC_CREAT | 0666);
	   printf("shmid in master is %d\n",shmid);
           if (-1 == shmid) {
               return -3;
           }


       }

	s_area = shmat(shmid, NULL, 0);
	printf("s_area after shmat(master) is %p\n", s_area);
	if (-1 == (long)s_area) {
		return -4;
	}

	return 0;

}


static int compare(const void *_a, const void *_b){

	uint64_t a = *((uint64_t *)_a);
	uint64_t b = *((uint64_t *)_b);

	if(a < b)
		return -1;
	if(a > b)
		return 1;
	return 0;
}



static void dump_time(unsigned long*  timekeeper)
{
	int i;
	unsigned long long counter = 0;
        uint64_t min;
	uint64_t max;
	if (timekeeper == NULL) {
		printf("Time keeper was null, ret");
		return;
	}
	for (i = 1; i < NUM_LOOPS; i++) {
		printf("%lu\n", timekeeper[i]);
		counter+= timekeeper[i];
	}

	qsort(timekeeper, NUM_LOOPS, sizeof(uint64_t), compare);

	min = timekeeper[0];
	max = timekeeper[NUM_LOOPS-1];
	counter = min;
	for (i = 1; i < NUM_LOOPS; i++) {
		//printf("%lu\n", timekeeper[i]);
		counter+= timekeeper[i];
	}
	printf("MIN\tMAX\tAVG\tMEDIAN\n");
	printf("%lu & %lu & %llu & %lu\n", min, max, counter/NUM_LOOPS, timekeeper[4999]);

}




int main(void)
{

	int ret;

	static	unsigned int send_slot_avail = 0xC1346BAD;
	static	unsigned int producer_send_msg = 0xBADBEEF;
	static  unsigned int msg_available = 0x13370000;
	struct ipc_message *prod_msg, *cons_msg;
	unsigned int count = 0;
	unsigned long  start64, end64;

	unsigned long *timekeeper = malloc(NUM_LOOPS * sizeof(unsigned long));
	if(!timekeeper)
		return EXIT_FAILURE;

	memset(timekeeper,0,NUM_LOOPS);

	ret = initialize_shm();
	if (ret != 0)
		return EXIT_FAILURE;


	prod_msg = s_area;
	cons_msg = s_area;//(struct ipc_message*) ((char *)s_area + (4096 * 50));

	if(nice(-20) != -20)
		perror("Couldn't nice!");


	while (count < NUM_LOOPS) {
		start64 = RDTSC_START();

		prod_msg->monitor = producer_send_msg;

		while(cons_msg->monitor != msg_available)
			asm volatile("pause" ::: "memory");

		//printf("master %d\n",count);
		end64 = RDTSCP();
		timekeeper[count] = end64-start64;
		count++;
	}

	dump_time(timekeeper);
	return EXIT_SUCCESS;
}