1 /**
2 Module used to store information about executed jobs. 
3 Information is stored using function pointer and times of start and end of a job.
4 
5 Data should be retrived by only one thread.
6 */
7 module mutils.job_manager.debug_data;
8 
9 //import std.datetime;
10 import core.time;
11 import mutils.container.vector;
12 import mutils.container_shared.shared_vector;
13 import std.experimental.allocator;
14 import std.experimental.allocator.mallocator;
15 
16 //Hepler types
17 alias ExecutionVector=Vector!Execution;
18 alias VectorOfExecutionVectors=LockedVector!ExecutionVector;
19 //static data
20 //StopWatch threadLocalWatch;
21 ExecutionVector threadLocalExecutions;
22 __gshared VectorOfExecutionVectors globalVectorOfExecutionVectors;
23 
24 //local data initialization
25 void initializeDebugData(){
26 	//threadLocalWatch.start();
27 	//threadLocalExecutions=Mallocator.instance.make!ExecutionVector;
28 	globalVectorOfExecutionVectors.add(threadLocalExecutions);
29 }
30 void deinitializeDebugData(){
31 	globalVectorOfExecutionVectors.removeElement(threadLocalExecutions);
32 	//Mallocator.instance.dispose(threadLocalExecutions);
33 }
34 
35 //shared data initialization
36 shared static this(){
37 	globalVectorOfExecutionVectors=Mallocator.instance.make!VectorOfExecutionVectors;
38 }
39 shared static ~this(){
40 	Mallocator.instance.dispose(globalVectorOfExecutionVectors);
41 }
42 
43 //main functionality
44 void storeExecution(Execution exec){
45 	threadLocalExecutions~=exec;
46 }
47 //thread unsafe
48 void resetExecutions(){
49 	foreach(executions;globalVectorOfExecutionVectors[]){
50 		executions.reset();
51 	}
52 }
53 import std.algorithm:map,joiner;
54 //thread unsafe
55 auto getExecutions(){
56 	return globalVectorOfExecutionVectors[].map!((a) => a[]).joiner;
57 }
58 
59 
60 struct Execution{
61 	void* funcAddr;
62 	long startTime;
63 	long endTime;
64 	this(void* funcAddr){
65 		this.funcAddr=funcAddr;
66 		startTime=MonoTime.currTime.ticks;
67 	}
68 	void end(){
69 		endTime=MonoTime.currTime.ticks;
70 	}
71 	long dt(){
72 		return endTime-startTime;
73 	}
74 	long ticksPerSecond(){
75 		return MonoTime.ticksPerSecond;
76 	}
77 }