AbstractGraalDumper.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.salver.dumper;
24
25import java.io.IOException;
26
27import org.graalvm.compiler.salver.Salver;
28import org.graalvm.compiler.salver.data.DataDict;
29
30public class AbstractGraalDumper extends AbstractSerializerDumper {
31
32    public static final String EVENT_NAMESPACE = "graal";
33
34    private int eventCounter;
35
36    public void beginDump() throws IOException {
37        beginDump(EVENT_NAMESPACE);
38    }
39
40    protected void beginDump(String namespace) throws IOException {
41        beginDump(namespace, getBeginDumpDataDict());
42    }
43
44    protected void beginDump(String namespace, DataDict dataDict) throws IOException {
45        DataDict eventDict = createEventDict(":begin");
46        eventDict.put("@time", System.currentTimeMillis());
47        eventDict.put("@ecid", Salver.ECID);
48        if (namespace != null) {
49            eventDict.put("@namespace", namespace);
50        }
51        if (dataDict != null) {
52            eventDict.put("@data", dataDict);
53        }
54        serializeAndFlush(eventDict);
55    }
56
57    protected DataDict getBeginDumpDataDict() {
58        DataDict dataDict = new DataDict();
59        dataDict.put("dumper", getClass().getSimpleName());
60        dataDict.put("thread", Thread.currentThread().getName());
61        return dataDict;
62    }
63
64    public void endDump() throws IOException {
65        DataDict eventDict = createEventDict(":end");
66        eventDict.put("@time", System.currentTimeMillis());
67        serializeAndFlush(eventDict);
68    }
69
70    @Override
71    public void close() throws IOException {
72        endDump();
73    }
74
75    protected DataDict createEventDict(String name) {
76        DataDict eventDict = new DataDict();
77        eventDict.put("@event", name);
78        eventDict.put("@n", eventCounter++);
79        return eventDict;
80    }
81
82    protected DataDict createEventDict(String name, DataDict data) {
83        DataDict eventDict = createEventDict(name);
84        eventDict.put("@data", data);
85        return eventDict;
86    }
87}
88