Browse Source

allow to start profiling from code

Nicolas Cannasse 5 years ago
parent
commit
86dace5013
3 changed files with 26 additions and 8 deletions
  1. 1 1
      src/hlmodule.h
  2. 2 2
      src/main.c
  3. 23 5
      src/profile.c

+ 1 - 1
src/hlmodule.h

@@ -149,7 +149,7 @@ h_bool hl_module_patch( hl_module *m, hl_code *code );
 void hl_module_free( hl_module *m );
 void hl_module_free( hl_module *m );
 h_bool hl_module_debug( hl_module *m, int port, h_bool wait );
 h_bool hl_module_debug( hl_module *m, int port, h_bool wait );
 
 
-void hl_profile_start( int sample_count );
+void hl_profile_setup( int sample_count );
 void hl_profile_end();
 void hl_profile_end();
 
 
 jit_ctx *hl_jit_alloc();
 jit_ctx *hl_jit_alloc();

+ 2 - 2
src/main.c

@@ -228,9 +228,9 @@ int main(int argc, pchar *argv[]) {
 	ctx.c.fun = ctx.m->functions_ptrs[ctx.m->code->entrypoint];
 	ctx.c.fun = ctx.m->functions_ptrs[ctx.m->code->entrypoint];
 	ctx.c.hasValue = 0;
 	ctx.c.hasValue = 0;
 	setup_handler();
 	setup_handler();
-	if( profile_count > 0 ) hl_profile_start(profile_count);
+	hl_profile_setup(profile_count);
 	ctx.ret = hl_dyn_call_safe(&ctx.c,NULL,0,&isExc);
 	ctx.ret = hl_dyn_call_safe(&ctx.c,NULL,0,&isExc);
-	if( profile_count > 0 ) hl_profile_end();
+	hl_profile_end();
 	if( isExc ) {
 	if( isExc ) {
 		varray *a = hl_exception_stack();
 		varray *a = hl_exception_stack();
 		int i;
 		int i;

+ 23 - 5
src/profile.c

@@ -168,7 +168,7 @@ static void hl_profile_loop( void *_ ) {
 	while( !data.stopLoop ) {
 	while( !data.stopLoop ) {
 		double t = hl_sys_time();
 		double t = hl_sys_time();
 		if( t < next || data.profiling_pause ) {
 		if( t < next || data.profiling_pause ) {
-			if( !(t < next) ) next = t; 
+			if( !(t < next) ) next = t;
 			data.waitLoop = false;
 			data.waitLoop = false;
 			continue;
 			continue;
 		}
 		}
@@ -179,7 +179,7 @@ static void hl_profile_loop( void *_ ) {
 		for(i=0;i<threads->count;i++) {
 		for(i=0;i<threads->count;i++) {
 			hl_thread_info *t = threads->threads[i];
 			hl_thread_info *t = threads->threads[i];
 			if( t->flags & HL_THREAD_INVISIBLE ) continue;
 			if( t->flags & HL_THREAD_INVISIBLE ) continue;
-			
+
 			if( !cur || cur->tid != t->thread_id ) {
 			if( !cur || cur->tid != t->thread_id ) {
 				thread_handle *h = malloc(sizeof(thread_handle));
 				thread_handle *h = malloc(sizeof(thread_handle));
 				h->tid = t->thread_id;
 				h->tid = t->thread_id;
@@ -202,15 +202,25 @@ static void hl_profile_loop( void *_ ) {
 		}
 		}
 		next += wait_time;
 		next += wait_time;
 	}
 	}
+	free(data.tmpMemory);
+	data.tmpMemory = NULL;
+	data.sample_count = 0;
+	data.stopLoop = false;
 }
 }
 
 
 static void profile_event( int code, vbyte *data, int dataLen );
 static void profile_event( int code, vbyte *data, int dataLen );
 
 
-void hl_profile_start( int sample_count ) {
+void hl_profile_setup( int sample_count ) {
 #	if defined(HL_THREADS) && defined(HL_WIN_DESKTOP)
 #	if defined(HL_THREADS) && defined(HL_WIN_DESKTOP)
+	hl_setup_profiler(profile_event,hl_profile_end);
+	if( data.sample_count ) return;
+	if( sample_count < 0 ) {
+		// was not started with --profile : pause until we get start event
+		data.profiling_pause++;
+		return;
+	}
 	data.sample_count = sample_count;
 	data.sample_count = sample_count;
 	hl_thread_start(hl_profile_loop,NULL,false);
 	hl_thread_start(hl_profile_loop,NULL,false);
-	hl_setup_profiler(profile_event,hl_profile_end);
 #	endif
 #	endif
 }
 }
 
 
@@ -232,7 +242,7 @@ static bool read_profile_data( profile_reader *r, void *ptr, int size ) {
 
 
 static void profile_dump() {
 static void profile_dump() {
 	if( !data.first_record ) return;
 	if( !data.first_record ) return;
-	
+
 	data.profiling_pause++;
 	data.profiling_pause++;
 	printf("Writing profiling data...\n");
 	printf("Writing profiling data...\n");
 	fflush(stdout);
 	fflush(stdout);
@@ -317,7 +327,9 @@ static void profile_dump() {
 
 
 void hl_profile_end() {
 void hl_profile_end() {
 	profile_dump();
 	profile_dump();
+	if( !data.sample_count ) return;
 	data.stopLoop = true;
 	data.stopLoop = true;
+	while( data.stopLoop ) {};
 }
 }
 
 
 static void profile_event( int code, vbyte *ptr, int dataLen ) {
 static void profile_event( int code, vbyte *ptr, int dataLen ) {
@@ -352,6 +364,12 @@ static void profile_event( int code, vbyte *ptr, int dataLen ) {
 	case -6:
 	case -6:
 		profile_dump();
 		profile_dump();
 		break;
 		break;
+	case -7:
+		{
+			uchar *end = NULL;
+			hl_profile_setup( ptr ? utoi((uchar*)ptr,&end) : 1000);
+		}
+		break;
 	default:
 	default:
 		if( code < 0 ) return;
 		if( code < 0 ) return;
 		if( data.profiling_pause || (code != 0 && (hl_get_thread()->flags & HL_THREAD_PROFILER_PAUSED)) ) return;
 		if( data.profiling_pause || (code != 0 && (hl_get_thread()->flags & HL_THREAD_PROFILER_PAUSED)) ) return;