summaryrefslogtreecommitdiff
path: root/epilogue.c
blob: 108d78bf078a06c2cae489ab2a0effb6d9201035 (plain)
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

__GLXextFuncPtr glXGetProcAddressARB (const GLubyte * name)
{
	
}

__GLXextFuncPtr glXGetProcAddress(const GLubyte *name)
{
	return glXGetProcAddressARB(name);
}

typedef struct vertex_array_format
{
	struct
	{
		GLenum type;
		GLint size;
		GLint enabled;
	}
	attrib[16];
	uint64_t uses;
	uint64_t total_time;
	struct vertex_array_format* next;
}
vertex_array_format;

static vertex_array_format* format_list = NULL;

static vertex_array_format* vertex_array_get_format()
{
	int a, num_enabled = 0;
	vertex_array_format* f = (vertex_array_format*) malloc(sizeof(*f));
	for(a = 0 ; a < 16 ; a++)
	{
		glGetVertexAttribiv(a, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &f->attrib[a].enabled);
		if (f->attrib[a].enabled)
		{
			glGetVertexAttribiv(a, GL_VERTEX_ATTRIB_ARRAY_SIZE, &f->attrib[a].size);
			glGetVertexAttribiv(a, GL_VERTEX_ATTRIB_ARRAY_TYPE, &f->attrib[a].type);
			num_enabled++;
		}
		else
		{
			f->attrib[a].size = 0;
			f->attrib[a].type = 0;
		}
	}
	if (num_enabled == 0)
	{
		GLint save_active_texture;
		glGetIntegerv(GL_CLIENT_ACTIVE_TEXTURE, &save_active_texture);
		for(a = 0 ; a < 16 ; a++)
		{
			GLenum get_enabled, get_size, get_type;
			GLint enabled = -1, size = -1, type = -1;

			switch(a)
			{
				case  0:	get_enabled = GL_VERTEX_ARRAY; get_size = GL_VERTEX_ARRAY_SIZE; get_type = GL_VERTEX_ARRAY_TYPE; break;
				case  1:	get_enabled = GL_NORMAL_ARRAY; size = 3; get_type = GL_NORMAL_ARRAY_TYPE; break;
				case  2:	get_enabled = GL_COLOR_ARRAY; get_size = GL_COLOR_ARRAY_SIZE; get_type = GL_COLOR_ARRAY_TYPE; break;
				case  3:	get_enabled = GL_INDEX_ARRAY; size = 1; get_type = GL_INDEX_ARRAY_TYPE; break;
				case  4:	get_enabled = GL_EDGE_FLAG_ARRAY; size = 1; type = GL_UNSIGNED_BYTE; break;
				case  5:	get_enabled = GL_FOG_COORD_ARRAY; size = 1; get_type = GL_FOG_COORD_ARRAY_TYPE; break;
				case  6:	get_enabled = GL_SECONDARY_COLOR_ARRAY; size = 1; get_type = GL_FOG_COORD_ARRAY_TYPE; break;
				case  7:	enabled = 0; size = 0; type = 0; break;
				case  8:	get_enabled = GL_TEXTURE_COORD_ARRAY; get_size = GL_TEXTURE_COORD_ARRAY_SIZE; type = GL_TEXTURE_COORD_ARRAY_TYPE; break;
				case  9:	get_enabled = GL_TEXTURE_COORD_ARRAY; get_size = GL_TEXTURE_COORD_ARRAY_SIZE; type = GL_TEXTURE_COORD_ARRAY_TYPE; break;
				case 10:	get_enabled = GL_TEXTURE_COORD_ARRAY; get_size = GL_TEXTURE_COORD_ARRAY_SIZE; type = GL_TEXTURE_COORD_ARRAY_TYPE; break;
				case 11:	get_enabled = GL_TEXTURE_COORD_ARRAY; get_size = GL_TEXTURE_COORD_ARRAY_SIZE; type = GL_TEXTURE_COORD_ARRAY_TYPE; break;
				case 12:	get_enabled = GL_TEXTURE_COORD_ARRAY; get_size = GL_TEXTURE_COORD_ARRAY_SIZE; type = GL_TEXTURE_COORD_ARRAY_TYPE; break;
				case 13:	get_enabled = GL_TEXTURE_COORD_ARRAY; get_size = GL_TEXTURE_COORD_ARRAY_SIZE; type = GL_TEXTURE_COORD_ARRAY_TYPE; break;
				case 14:	get_enabled = GL_TEXTURE_COORD_ARRAY; get_size = GL_TEXTURE_COORD_ARRAY_SIZE; type = GL_TEXTURE_COORD_ARRAY_TYPE; break;
				case 15:	get_enabled = GL_TEXTURE_COORD_ARRAY; get_size = GL_TEXTURE_COORD_ARRAY_SIZE; type = GL_TEXTURE_COORD_ARRAY_TYPE; break;
			}

			if (a>=8)
			{
				glClientActiveTexture(GL_TEXTURE0+a-8);
			}
			if (enabled == -1)
				enabled = glIsEnabled(get_enabled);
			if (size == -1)
				glGetIntegerv(get_size, &size);
			if (type == -1)
				glGetIntegerv(get_type, &type);

			f->attrib[a].enabled = enabled;
			f->attrib[a].size = size;
			f->attrib[a].type = type;
		}
		glClientActiveTexture(save_active_texture);
	}

	f->uses = 0;
	f->total_time = 0;
	f->next = NULL;
	return f;
}

static vertex_array_format* vertex_array_find_format(vertex_array_format* to_find)
{
	vertex_array_format* f = format_list;
	while(f)
	{
		if(! memcmp(&f->attrib, &to_find->attrib, sizeof(f->attrib) ) )
		{
			free(to_find);
			return f;
		}

		f = f->next;
	}

	// not found allocate new
	to_find->next = format_list;
	format_list = to_find;
	return to_find;
}

static void vertex_array_add(int num_elements, uint64_t time)
{
	vertex_array_format *f, *g;
	f = vertex_array_get_format();
	g = vertex_array_find_format(f);
	g->uses += num_elements;
	g->total_time += time;
}



static void (*rtld_fini_func)(void);

static int compare(const void* a, const void* b)
{
	int v1 = *(int*)(a);
	int v2 = *(int*)(b);
	return total_time[v1]<total_time[v2]?1:-1;
}

static void gl_type(GLenum type, char* s)
{
	switch(type) 
	{
		case GL_BYTE:		sprintf(s, "B ");	break;
		case GL_UNSIGNED_BYTE:	sprintf(s, "UB");	break;
		case GL_SHORT:		sprintf(s, "S ");	break;
		case GL_UNSIGNED_SHORT:	sprintf(s, "US");	break;
		case GL_INT:		sprintf(s, "I ");	break;
		case GL_UNSIGNED_INT:	sprintf(s, "UI");	break;
		case GL_FLOAT:		sprintf(s, "F ");	break;
		case GL_DOUBLE:		sprintf(s, "D");	break;
	}
}

static void exit_func()
{
	rtld_fini_func();
	if (trace_summary)
	{
		uint64_t all = 0;
		int i;
		int* findex = (int*)malloc(sizeof(int)*num_funcs);

		for(i = 0 ; i < num_funcs ; i++)
		{
			all += total_time[i];
			findex[i] = i;
		}

		qsort(findex, num_funcs, sizeof(int), compare);

		printf("=============================================================\n");
		printf("Function Name  \t\t\ttotal(us) \ttotal(%%) \t   #calls\t    avg us\n");
		for(i = 0 ; i < num_funcs ; i++)
		{
			int real_index = findex[i];
			if (total_calls[real_index])
			{
				printf("%s",get_proc_list[real_index].name);
				int j;
				for(j=0;j<max(25-strlen(get_proc_list[real_index].name),0);j++)
					printf(" ");
				printf("\t%8llu \t%7.3f \t%9d \t%10.5f\n", total_time[real_index],total_time[real_index]*100.0/(float)all,total_calls[real_index],total_time[real_index]/(float)total_calls[real_index]);
			}
		}
		free(findex);
		printf("=============================================================\n");
		printf("Array format                                                       us per\tusage  \t total\n");
		printf("V   N   C   I   EF  FG  C2  ?   T0  T1  T2  T3  T4  T5  T6  T7     vertex\tpercent\t #vertex\n");

		uint64_t total_uses = 0;
		vertex_array_format* f = format_list;

		while(f)
		{
			total_uses += f->uses;

			f = f->next;
		}

		f = format_list;
		while(f)
		{
			int a;
			for(a = 0 ; a < 16 ; a++)
			{
				if (f->attrib[a].enabled)
				{
					char type_name[4];
					gl_type(f->attrib[a].type, type_name);
					printf("%d%s ",f->attrib[a].size,type_name);
				}
				else
					printf("    ");
			}
			printf("%10.5f\t%7.3f\t %d\n",f->total_time/(float)f->uses, f->uses*100.0/total_uses, f->uses);

			f = f->next;
		}
	}
}

/* Modify the __start function by overiding this function */
int __libc_start_main(int(*main_fct)(int, char **), int argc,
		char ** ubp_av,void (*init) (void), void (*fini) (void),
		void (*rtld_fini) (void),void (* stack_end)) {

	int (*func)(int(*)(int, char **), int, char **,
			void (*) (void), void (*) (void),
			void (*) (void), void(*));

	func  = dlsym(((void *) -1l), "__libc_start_main");

	static int loaded=0;
	if (loaded)
		return;
	loaded=1;

	/* open library (NOTE: you may need to change this filename!) */
	void* lib_handle_libGL = NULL;
	char* libGL_places[] = {
		"/usr/lib64/libGL.so.1",
		"/usr/lib64/mesa/libGL.so.1",
		"/usr/lib64/libGL.so",
		"/usr/lib/libGL.so.1",
		"/usr/lib/mesa/libGL.so.1",
		"/usr/lib/libGL.so",
		NULL
	};

	int i = 0;

	while(!lib_handle_libGL && libGL_places[i])
	{
		lib_handle_libGL = dlopen(libGL_places[i], RTLD_LAZY);
		i++;
	}

	if (!lib_handle_libGL){perror("Can't find host libGL");exit(0);}

	/* get the host getprocaddress function */
	lib_glXGetProcAddressARB = dlsym(lib_handle_libGL, "glXGetProcAddressARB");

	memset(total_time, 0, sizeof(total_time));
	memset(total_time, 0, sizeof(total_calls));

	functable_init();

//	int r= func(main_fct, argc, ubp_av, init, fini, rtld_fini, stack_end);
	rtld_fini_func = rtld_fini;
	int r= func(main_fct, argc, ubp_av, init, fini, exit_func, stack_end);
	return r;
}