]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.threadlog/src/org/simantics/threadlog/ThreadLog.java
Removed use of gnu.trove2 entirely from release/1.28.1
[simantics/platform.git] / bundles / org.simantics.threadlog / src / org / simantics / threadlog / ThreadLog.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.threadlog;
13
14 import java.io.BufferedInputStream;
15 import java.io.BufferedOutputStream;
16 import java.io.DataInput;
17 import java.io.DataInputStream;
18 import java.io.DataOutput;
19 import java.io.DataOutputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.StreamCorruptedException;
25 import java.util.ArrayList;
26
27 import gnu.trove.list.array.TDoubleArrayList;
28 import gnu.trove.list.array.TLongArrayList;
29
30 public class ThreadLog {
31
32     private static final transient String HDR             = "TLOG";
33     private static final transient int    CURRENT_VERSION = 1;
34
35     static ThreadLog defaultLog;
36
37     ArrayList<String> tasks = new ArrayList<String>();
38     TDoubleArrayList times = new TDoubleArrayList();
39     TLongArrayList threads = new TLongArrayList();
40
41     public double[] getTimes() {
42         return times.toArray();
43     }
44
45     public String[] getTasks() {
46         return tasks.toArray(new String[tasks.size()]);
47     }
48
49     public long[] getThreads() {
50         return threads.toArray();
51     }
52
53     private class TaskImpl implements Task {
54         public String name;
55         public long thread;
56         public long beginTime;
57
58         public TaskImpl(String name) {
59             this.name = name;
60             this.thread = Thread.currentThread().getId();
61             this.beginTime = System.nanoTime();
62         }
63
64         @Override
65         public void end() {
66             long endTime = System.nanoTime();
67             synchronized(tasks) {
68                 tasks.add(name);
69                 times.add(beginTime*1e-9);
70                 times.add(endTime*1e-9);
71                 threads.add(thread);
72             }
73         }
74     }
75
76     private static enum DummyTask implements Task {
77         INSTANCE;
78
79         @Override
80         public void end() {
81         }
82     }
83
84     public Task begin(String name) {
85         return new TaskImpl(name);
86     }
87
88     public static Task BEGIN(String name) {
89         try {
90             if(defaultLog != null)
91                 return defaultLog.begin(name);
92         } catch(NullPointerException e) {
93         }
94         return DummyTask.INSTANCE;
95     }
96
97     public static ThreadLog setDefaultThreadLog(ThreadLog log) {
98         ThreadLog currentLog = defaultLog;
99         defaultLog = log;
100         return currentLog;
101     }
102
103     // ------------------------------------------------------------------------
104     // SERIALIZATION
105     // ------------------------------------------------------------------------
106
107     public static ThreadLog deserialize(File file) throws IOException {
108         DataInputStream in = null;
109         try {
110             in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
111             ThreadLog log = new ThreadLog();
112             log.doDeserialize(in);
113             return log;
114         } finally {
115             if (in != null) {
116                 in.close();
117             }
118         }
119     }
120
121     public static ThreadLog deserialize(DataInput in) throws IOException {
122         ThreadLog log = new ThreadLog();
123         log.doDeserialize(in);
124         return log;
125     }
126
127     private void doDeserialize(DataInput in) throws IOException {
128         String hdr = in.readUTF();
129         if (!HDR.equals(hdr)) {
130             throw new StreamCorruptedException("invalid header '" + hdr + "', expected " + HDR);
131         }
132         int ver = in.readInt();
133         if (ver == CURRENT_VERSION) {
134             int taskCount = in.readInt();
135             for (int i = 0; i < taskCount; ++i) {
136                 String task = in.readUTF();
137                 double beginTime = in.readDouble();
138                 double endTime = in.readDouble();
139                 long threadId = in.readLong();
140                 tasks.add(task);
141                 times.add(beginTime);
142                 times.add(endTime);
143                 threads.add(threadId);
144             }
145         } else {
146             throw new StreamCorruptedException("unrecognized log version: " + ver);
147         }
148     }
149
150     public void serialize(File file) throws IOException {
151         DataOutputStream out = null;
152         try {
153             out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
154             serialize(out);
155         } finally {
156             if (out != null) {
157                 out.close();
158             }
159         }
160     }
161
162     public void serialize(DataOutput out) throws IOException {
163         out.writeUTF(HDR);
164         out.writeInt(CURRENT_VERSION);
165         int len = tasks.size();
166         out.writeInt(len);
167         for (int i = 0; i < len; ++i) {
168             out.writeUTF(tasks.get(i));
169             out.writeDouble(times.getQuick(i*2));
170             out.writeDouble(times.getQuick(i*2+1));
171             out.writeLong(threads.getQuick(i));
172         }
173     }
174
175 }