1 /****************************************************************************
2 * Copyright (C) 2016 Intel Corporation. All Rights Reserved.
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:
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
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
25 * @brief Event handler interface. auto-generated file
29 * Generation Command Line:
30 * ${'\n* '.join(cmdline)}
32 ******************************************************************************/
35 #include "common/os.h"
36 #include "${event_header}"
43 //////////////////////////////////////////////////////////////////////////
44 /// EventHandlerFile - interface for handling events.
45 //////////////////////////////////////////////////////////////////////////
46 class EventHandlerFile : public EventHandler
49 EventHandlerFile(uint32_t id)
53 DWORD pid = GetCurrentProcessId();
54 TCHAR procname[MAX_PATH];
55 GetModuleFileName(NULL, procname, MAX_PATH);
56 const char* pBaseName = strrchr(procname, '\\');
57 std::stringstream outDir;
58 outDir << KNOB_DEBUG_OUTPUT_DIR << pBaseName << "_" << pid << std::ends;
59 CreateDirectory(outDir.str().c_str(), NULL);
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 std::stringstream fstr;
65 fstr << outDir.str().c_str() << "\\ar_event" << std::this_thread::get_id();
66 fstr << "_" << id << ".bin" << std::ends;
67 mFilename = fstr.str();
69 // There could be multiple threads creating thread pools. We
70 // want to make sure they are uniquly identified by adding in
71 // the creator's thread id into the filename.
72 std::stringstream fstr;
73 fstr << "/tmp/ar_event" << std::this_thread::get_id();
74 fstr << "_" << id << ".bin" << std::ends;
75 mFilename = fstr.str();
79 virtual ~EventHandlerFile()
84 //////////////////////////////////////////////////////////////////////////
85 /// @brief Flush buffer to file.
90 if (mBufOffset == mHeaderBufOffset)
92 // Nothing to flush. Only header has been generated.
97 file.open(mFilename, std::ios::out | std::ios::app | std::ios::binary);
101 SWR_INVALID("ArchRast: Could not open event file!");
105 file.write((char*)mBuffer, mBufOffset);
109 mHeaderBufOffset = 0; // Reset header offset so its no longer considered.
114 //////////////////////////////////////////////////////////////////////////
115 /// @brief Write event and its payload to the memory buffer.
116 void Write(uint32_t eventId, const char* pBlock, uint32_t size)
118 if ((mBufOffset + size + sizeof(eventId)) > mBufferSize)
122 // Don't corrupt what's already in the buffer?
123 /// @todo Maybe add corrupt marker to buffer here in case we can open file in future?
128 memcpy(&mBuffer[mBufOffset], (char*)&eventId, sizeof(eventId));
129 mBufOffset += sizeof(eventId);
130 memcpy(&mBuffer[mBufOffset], pBlock, size);
134 % for name in protos['event_names']:
135 //////////////////////////////////////////////////////////////////////////
136 /// @brief Handle ${name} event
137 virtual void Handle(const ${name}& event)
139 % if protos['events'][name]['num_fields'] == 0:
140 Write(${protos['events'][name]['event_id']}, (char*)&event.data, 0);
142 Write(${protos['events'][name]['event_id']}, (char*)&event.data, sizeof(event.data));
147 //////////////////////////////////////////////////////////////////////////
148 /// @brief Everything written to buffer this point is the header.
149 virtual void MarkHeader()
151 mHeaderBufOffset = mBufOffset;
154 std::string mFilename;
156 static const uint32_t mBufferSize = 1024;
157 uint8_t mBuffer[mBufferSize];
158 uint32_t mBufOffset{0};
159 uint32_t mHeaderBufOffset{0};