1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.threadlog;
\r
14 import gnu.trove.TDoubleArrayList;
\r
15 import gnu.trove.TLongArrayList;
\r
17 import java.io.BufferedInputStream;
\r
18 import java.io.BufferedOutputStream;
\r
19 import java.io.DataInput;
\r
20 import java.io.DataInputStream;
\r
21 import java.io.DataOutput;
\r
22 import java.io.DataOutputStream;
\r
23 import java.io.File;
\r
24 import java.io.FileInputStream;
\r
25 import java.io.FileOutputStream;
\r
26 import java.io.IOException;
\r
27 import java.io.StreamCorruptedException;
\r
28 import java.util.ArrayList;
\r
30 public class ThreadLog {
\r
32 private static final transient String HDR = "TLOG";
\r
33 private static final transient int CURRENT_VERSION = 1;
\r
35 static ThreadLog defaultLog;
\r
37 ArrayList<String> tasks = new ArrayList<String>();
\r
38 TDoubleArrayList times = new TDoubleArrayList();
\r
39 TLongArrayList threads = new TLongArrayList();
\r
41 public double[] getTimes() {
\r
42 return times.toNativeArray();
\r
45 public String[] getTasks() {
\r
46 return tasks.toArray(new String[tasks.size()]);
\r
49 public long[] getThreads() {
\r
50 return threads.toNativeArray();
\r
53 private class TaskImpl implements Task {
\r
56 public long beginTime;
\r
58 public TaskImpl(String name) {
\r
60 this.thread = Thread.currentThread().getId();
\r
61 this.beginTime = System.nanoTime();
\r
66 long endTime = System.nanoTime();
\r
67 synchronized(tasks) {
\r
69 times.add(beginTime*1e-9);
\r
70 times.add(endTime*1e-9);
\r
71 threads.add(thread);
\r
76 private static enum DummyTask implements Task {
\r
84 public Task begin(String name) {
\r
85 return new TaskImpl(name);
\r
88 public static Task BEGIN(String name) {
\r
90 if(defaultLog != null)
\r
91 return defaultLog.begin(name);
\r
92 } catch(NullPointerException e) {
\r
94 return DummyTask.INSTANCE;
\r
97 public static ThreadLog setDefaultThreadLog(ThreadLog log) {
\r
98 ThreadLog currentLog = defaultLog;
\r
103 // ------------------------------------------------------------------------
\r
105 // ------------------------------------------------------------------------
\r
107 public static ThreadLog deserialize(File file) throws IOException {
\r
108 DataInputStream in = null;
\r
110 in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
\r
111 ThreadLog log = new ThreadLog();
\r
112 log.doDeserialize(in);
\r
121 public static ThreadLog deserialize(DataInput in) throws IOException {
\r
122 ThreadLog log = new ThreadLog();
\r
123 log.doDeserialize(in);
\r
127 private void doDeserialize(DataInput in) throws IOException {
\r
128 String hdr = in.readUTF();
\r
129 if (!HDR.equals(hdr)) {
\r
130 throw new StreamCorruptedException("invalid header '" + hdr + "', expected " + HDR);
\r
132 int ver = in.readInt();
\r
133 if (ver == CURRENT_VERSION) {
\r
134 int taskCount = in.readInt();
\r
135 for (int i = 0; i < taskCount; ++i) {
\r
136 String task = in.readUTF();
\r
137 double beginTime = in.readDouble();
\r
138 double endTime = in.readDouble();
\r
139 long threadId = in.readLong();
\r
141 times.add(beginTime);
\r
142 times.add(endTime);
\r
143 threads.add(threadId);
\r
146 throw new StreamCorruptedException("unrecognized log version: " + ver);
\r
150 public void serialize(File file) throws IOException {
\r
151 DataOutputStream out = null;
\r
153 out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
\r
162 public void serialize(DataOutput out) throws IOException {
\r
164 out.writeInt(CURRENT_VERSION);
\r
165 int len = tasks.size();
\r
167 for (int i = 0; i < len; ++i) {
\r
168 out.writeUTF(tasks.get(i));
\r
169 out.writeDouble(times.getQuick(i*2));
\r
170 out.writeDouble(times.getQuick(i*2+1));
\r
171 out.writeLong(threads.getQuick(i));
\r