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.algorithm : map, joiner;
10 import std.experimental.allocator;
11 import std.experimental.allocator.mallocator;
12 
13 import mutils.container.vector;
14 import mutils.container_shared.shared_vector;
15 import mutils.time;
16 
17 //Hepler types
18 alias ExecutionVector = Vector!Execution;
19 alias VectorOfExecutionVectors = LockedVector!ExecutionVector;
20 //static data
21 //StopWatch threadLocalWatch;
22 ExecutionVector threadLocalExecutions;
23 __gshared VectorOfExecutionVectors globalVectorOfExecutionVectors;
24 
25 //local data initialization
26 void initializeDebugData() {
27 	//threadLocalWatch.start();
28 	//threadLocalExecutions=Mallocator.instance.make!ExecutionVector;
29 	globalVectorOfExecutionVectors.add(threadLocalExecutions);
30 }
31 
32 void deinitializeDebugData() {
33 	globalVectorOfExecutionVectors.removeElement(threadLocalExecutions);
34 	//Mallocator.instance.dispose(threadLocalExecutions);
35 }
36 
37 //shared data initialization
38 shared static this() {
39 	globalVectorOfExecutionVectors = Mallocator.instance.make!VectorOfExecutionVectors;
40 }
41 
42 shared static ~this() {
43 	Mallocator.instance.dispose(globalVectorOfExecutionVectors);
44 }
45 
46 //main functionality
47 void storeExecution(Execution exec) {
48 	threadLocalExecutions ~= exec;
49 }
50 //thread unsafe
51 void resetExecutions() {
52 	foreach (executions; globalVectorOfExecutionVectors[]) {
53 		executions.reset();
54 	}
55 }
56 
57 //thread unsafe
58 auto getExecutions() {
59 	return globalVectorOfExecutionVectors[].map!((a) => a[]).joiner;
60 }
61 
62 struct Execution {
63 	void* funcAddr;
64 	long startTime;
65 	long endTime;
66 	this(void* funcAddr) {
67 		this.funcAddr = funcAddr;
68 		startTime = useconds();
69 	}
70 
71 	void end() {
72 		endTime = useconds();
73 	}
74 
75 	long dt() {
76 		return endTime - startTime;
77 	}
78 
79 	long ticksPerSecond() {
80 		return 1_000_000;
81 	}
82 }