9017e8dc7d8163865d43f6f2986bc51e874855cf
[mesa.git] / src / gallium / drivers / swr / rasterizer / codegen / templates / gen_ar_eventhandlerfile.hpp
1 /****************************************************************************
2 * Copyright (C) 2016 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 ${filename}
24 *
25 * @brief Event handler interface. auto-generated file
26 *
27 * DO NOT EDIT
28 *
29 * Generation Command Line:
30 * ${'\n* '.join(cmdline)}
31 *
32 ******************************************************************************/
33 #pragma once
34
35 #include "common/os.h"
36 #include "${event_header}"
37 #include <fstream>
38 #include <sstream>
39
40 namespace ArchRast
41 {
42 //////////////////////////////////////////////////////////////////////////
43 /// EventHandlerFile - interface for handling events.
44 //////////////////////////////////////////////////////////////////////////
45 class EventHandlerFile : public EventHandler
46 {
47 public:
48 EventHandlerFile(uint32_t id)
49 : mBufOffset(0)
50 {
51 #if defined(_WIN32)
52 DWORD pid = GetCurrentProcessId();
53 TCHAR procname[MAX_PATH];
54 GetModuleFileName(NULL, procname, MAX_PATH);
55 const char* pBaseName = strrchr(procname, '\\');
56 std::stringstream outDir;
57 outDir << KNOB_DEBUG_OUTPUT_DIR << pBaseName << "_" << pid << std::ends;
58 CreateDirectory(outDir.str().c_str(), NULL);
59
60 char buf[255];
61 // There could be multiple threads creating thread pools. We
62 // want to make sure they are uniquly identified by adding in
63 // the creator's thread id into the filename.
64 sprintf(buf, "%s\\ar_event%d_%d.bin", outDir.str().c_str(), GetCurrentThreadId(), id);
65 mFilename = std::string(buf);
66 #else
67 char buf[255];
68 // There could be multiple threads creating thread pools. We
69 // want to make sure they are uniquly identified by adding in
70 // the creator's thread (process) id into the filename.
71 // Assumes a 1:1 thread:LWP mapping as in linux.
72 sprintf(buf, "%s/ar_event%d_%d.bin", "/tmp", GetCurrentProcessId(), id);
73 mFilename = std::string(buf);
74 #endif
75 }
76
77 virtual ~EventHandlerFile()
78 {
79 FlushBuffer();
80 }
81
82 //////////////////////////////////////////////////////////////////////////
83 /// @brief Flush buffer to file.
84 bool FlushBuffer()
85 {
86 if (mBufOffset > 0)
87 {
88 if (mBufOffset == mHeaderBufOffset)
89 {
90 // Nothing to flush. Only header has been generated.
91 return false;
92 }
93
94 std::ofstream file;
95 file.open(mFilename, std::ios::out | std::ios::app | std::ios::binary);
96
97 if (!file.is_open())
98 {
99 SWR_INVALID("ArchRast: Could not open event file!");
100 return false;
101 }
102
103 file.write((char*)mBuffer, mBufOffset);
104 file.close();
105
106 mBufOffset = 0;
107 mHeaderBufOffset = 0; // Reset header offset so its no longer considered.
108 }
109 return true;
110 }
111
112 //////////////////////////////////////////////////////////////////////////
113 /// @brief Write event and its payload to the memory buffer.
114 void Write(uint32_t eventId, const char* pBlock, uint32_t size)
115 {
116 if ((mBufOffset + size + sizeof(eventId)) > mBufferSize)
117 {
118 if (!FlushBuffer())
119 {
120 // Don't corrupt what's already in the buffer?
121 /// @todo Maybe add corrupt marker to buffer here in case we can open file in future?
122 return;
123 }
124 }
125
126 memcpy(&mBuffer[mBufOffset], (char*)&eventId, sizeof(eventId));
127 mBufOffset += sizeof(eventId);
128 memcpy(&mBuffer[mBufOffset], pBlock, size);
129 mBufOffset += size;
130 }
131
132 % for name in protos['event_names']:
133 //////////////////////////////////////////////////////////////////////////
134 /// @brief Handle ${name} event
135 virtual void Handle(const ${name}& event)
136 {
137 % if protos['events'][name]['num_fields'] == 0:
138 Write(${protos['events'][name]['event_id']}, (char*)&event.data, 0);
139 % else:
140 Write(${protos['events'][name]['event_id']}, (char*)&event.data, sizeof(event.data));
141 %endif
142 }
143 % endfor
144
145 //////////////////////////////////////////////////////////////////////////
146 /// @brief Everything written to buffer this point is the header.
147 virtual void MarkHeader()
148 {
149 mHeaderBufOffset = mBufOffset;
150 }
151
152 std::string mFilename;
153
154 static const uint32_t mBufferSize = 1024;
155 uint8_t mBuffer[mBufferSize];
156 uint32_t mBufOffset{0};
157 uint32_t mHeaderBufOffset{0};
158 };
159 }