1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.threadlog;
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;
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;
27 import gnu.trove.list.array.TDoubleArrayList;
28 import gnu.trove.list.array.TLongArrayList;
30 public class ThreadLog {
32 private static final transient String HDR = "TLOG";
33 private static final transient int CURRENT_VERSION = 1;
35 static ThreadLog defaultLog;
37 ArrayList<String> tasks = new ArrayList<String>();
38 TDoubleArrayList times = new TDoubleArrayList();
39 TLongArrayList threads = new TLongArrayList();
41 public double[] getTimes() {
42 return times.toArray();
45 public String[] getTasks() {
46 return tasks.toArray(new String[tasks.size()]);
49 public long[] getThreads() {
50 return threads.toArray();
53 private class TaskImpl implements Task {
56 public long beginTime;
58 public TaskImpl(String name) {
60 this.thread = Thread.currentThread().getId();
61 this.beginTime = System.nanoTime();
66 long endTime = System.nanoTime();
69 times.add(beginTime*1e-9);
70 times.add(endTime*1e-9);
76 private static enum DummyTask implements Task {
84 public Task begin(String name) {
85 return new TaskImpl(name);
88 public static Task BEGIN(String name) {
90 if(defaultLog != null)
91 return defaultLog.begin(name);
92 } catch(NullPointerException e) {
94 return DummyTask.INSTANCE;
97 public static ThreadLog setDefaultThreadLog(ThreadLog log) {
98 ThreadLog currentLog = defaultLog;
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
107 public static ThreadLog deserialize(File file) throws IOException {
108 DataInputStream in = null;
110 in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
111 ThreadLog log = new ThreadLog();
112 log.doDeserialize(in);
121 public static ThreadLog deserialize(DataInput in) throws IOException {
122 ThreadLog log = new ThreadLog();
123 log.doDeserialize(in);
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);
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();
141 times.add(beginTime);
143 threads.add(threadId);
146 throw new StreamCorruptedException("unrecognized log version: " + ver);
150 public void serialize(File file) throws IOException {
151 DataOutputStream out = null;
153 out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
162 public void serialize(DataOutput out) throws IOException {
164 out.writeInt(CURRENT_VERSION);
165 int len = tasks.size();
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));