1/****************************************************************************
2* Copyright (C) 2014-2015 Intel Corporation.   All Rights Reserved.
3*
4* Permission is hereby granted, free of charge, to any person obtaining a
5* copy of this software and associated documentation files (the "Software"),
6* to deal in the Software without restriction, including without limitation
7* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8* and/or sell copies of the Software, and to permit persons to whom the
9* Software is furnished to do so, subject to the following conditions:
10*
11* The above copyright notice and this permission notice (including the next
12* paragraph) shall be included in all copies or substantial portions of the
13* Software.
14*
15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21* IN THE SOFTWARE.
22*
23* @file rdtsc_buckets.h
24*
25* @brief declaration for rdtsc buckets.
26*
27* Notes:
28*
29******************************************************************************/
30#pragma once
31
32#include <vector>
33#include <cassert>
34
35struct BUCKET
36{
37    uint32_t id{ 0 };
38    uint64_t start{ 0 };
39    uint64_t elapsed{ 0 };
40    uint32_t count{ 0 };
41
42    BUCKET* pParent{ nullptr };
43    std::vector<BUCKET> children;
44};
45
46struct BUCKET_DESC
47{
48    // name of bucket, used in reports
49    std::string name;
50
51    // description of bucket, used in threadviz
52    std::string description;
53
54    // enable for threadviz dumping
55    bool enableThreadViz;
56
57    // threadviz color of bucket, in RGBA8_UNORM format
58    uint32_t color;
59};
60
61
62struct BUCKET_THREAD
63{
64    // name of thread, used in reports
65    std::string name;
66
67    // id for this thread, assigned by the thread manager
68    uint32_t id{ 0 };
69
70    // root of the bucket hierarchy for this thread
71    BUCKET root;
72
73    // currently executing bucket somewhere in the hierarchy
74    BUCKET* pCurrent{ nullptr };
75
76    // currently executing hierarchy level
77    uint32_t level{ 0 };
78
79    // threadviz file object
80    FILE* vizFile{ nullptr };
81
82
83    BUCKET_THREAD() {}
84    BUCKET_THREAD(const BUCKET_THREAD& that)
85    {
86        name = that.name;
87        id = that.id;
88        root = that.root;
89        pCurrent = &root;
90        vizFile = that.vizFile;
91    }
92};
93
94enum VIZ_TYPE
95{
96    VIZ_START = 0,
97    VIZ_STOP  = 1,
98    VIZ_DATA  = 2
99};
100
101struct VIZ_START_DATA
102{
103    uint8_t type;
104    uint32_t bucketId;
105    uint64_t timestamp;
106};
107
108struct VIZ_STOP_DATA
109{
110    uint8_t type;
111    uint64_t timestamp;
112};
113
114inline void Serialize(FILE* f, const VIZ_START_DATA& data)
115{
116    fwrite(&data, sizeof(VIZ_START_DATA), 1, f);
117}
118
119inline void Deserialize(FILE* f, VIZ_START_DATA& data)
120{
121    fread(&data, sizeof(VIZ_START_DATA), 1, f);
122    assert(data.type == VIZ_START);
123}
124
125inline void Serialize(FILE* f, const VIZ_STOP_DATA& data)
126{
127    fwrite(&data, sizeof(VIZ_STOP_DATA), 1, f);
128}
129
130inline void Deserialize(FILE* f, VIZ_STOP_DATA& data)
131{
132    fread(&data, sizeof(VIZ_STOP_DATA), 1, f);
133    assert(data.type == VIZ_STOP);
134}
135
136inline void Serialize(FILE* f, const std::string& string)
137{
138    assert(string.size() <= 256);
139
140    uint8_t length = (uint8_t)string.size();
141    fwrite(&length, sizeof(length), 1, f);
142    fwrite(string.c_str(), string.size(), 1, f);
143}
144
145inline void Deserialize(FILE* f, std::string& string)
146{
147    char cstr[256];
148    uint8_t length;
149    fread(&length, sizeof(length), 1, f);
150    fread(cstr, length, 1, f);
151    cstr[length] = 0;
152    string.assign(cstr);
153}
154
155inline void Serialize(FILE* f, const BUCKET_DESC& desc)
156{
157    Serialize(f, desc.name);
158    Serialize(f, desc.description);
159    fwrite(&desc.enableThreadViz, sizeof(desc.enableThreadViz), 1, f);
160    fwrite(&desc.color, sizeof(desc.color), 1, f);
161}
162
163inline void Deserialize(FILE* f, BUCKET_DESC& desc)
164{
165    Deserialize(f, desc.name);
166    Deserialize(f, desc.description);
167    fread(&desc.enableThreadViz, sizeof(desc.enableThreadViz), 1, f);
168    fread(&desc.color, sizeof(desc.color), 1, f);
169}
170