xref: /llvm-project/llvm/tools/llvm-exegesis/lib/SubprocessMemory.h (revision 50e6218132d6d9cfc2645fa19cf72d0437a62d95)
1 //===-- SubprocessMemory.h --------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Defines a class that automatically handles auxiliary memory and the
11 /// underlying shared memory backings for memory definitions
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_SUBPROCESSMEMORY_H
16 #define LLVM_TOOLS_LLVM_EXEGESIS_SUBPROCESSMEMORY_H
17 
18 #include "BenchmarkResult.h"
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #ifdef _MSC_VER
24 typedef int pid_t;
25 #else
26 #include <sys/types.h>
27 #endif // _MSC_VER
28 
29 
30 namespace llvm {
31 namespace exegesis {
32 
33 class SubprocessMemory {
34 public:
35   static constexpr const size_t AuxiliaryMemoryOffset = 1;
36   static constexpr const size_t AuxiliaryMemorySize = 4096;
37 
38   // Gets the thread ID for the calling thread.
39   static long getCurrentTID();
40 
41   Error initializeSubprocessMemory(pid_t ProcessID);
42 
43   // The following function sets up memory definitions. It creates shared
44   // memory objects for the definitions and fills them with the specified
45   // values. Arguments: MemoryDefinitions - A map from memory value names to
46   // MemoryValues, ProcessID - The ID of the current process.
47   Error addMemoryDefinition(
48       std::unordered_map<std::string, MemoryValue> MemoryDefinitions,
49       pid_t ProcessID);
50 
51   // The following function sets up the auxiliary memory by opening shared
52   // memory objects backing memory definitions and putting file descriptors
53   // into appropriate places. Arguments: MemoryDefinitions - A map from memory
54   // values names to Memoryvalues, ParentPID - The ID of the process that
55   // setup the memory definitions, CounterFileDescriptor - The file descriptor
56   // for the performance counter that will be placed in the auxiliary memory
57   // section.
58   static Expected<int> setupAuxiliaryMemoryInSubprocess(
59       std::unordered_map<std::string, MemoryValue> MemoryDefinitions,
60       pid_t ParentPID, long ParentTID, int CounterFileDescriptor);
61 
62   ~SubprocessMemory();
63 
64 private:
65   std::vector<std::string> SharedMemoryNames;
66 };
67 
68 } // namespace exegesis
69 } // namespace llvm
70 
71 #endif
72