CIRCT 22.0.0git
Loading...
Searching...
No Matches
Services.h
Go to the documentation of this file.
1//===- StdServices.h - ESI standard services C++ API ------------*- 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// The APIs in this backend are all optionally implemented. The lower level
10// ones, however, are strongly recommended. 'Services' here refers to ESI
11// services. These are standard APIs into the standard ESI services.
12//
13// DO NOT EDIT!
14// This file is distributed as part of an ESI package. The source for this file
15// should always be modified within CIRCT.
16//
17//===----------------------------------------------------------------------===//
18
19// NOLINTNEXTLINE(llvm-header-guard)
20#ifndef ESI_RUNTIME_SERVICES_H
21#define ESI_RUNTIME_SERVICES_H
22
23#include "esi/Common.h"
24#include "esi/Context.h"
25#include "esi/Ports.h"
26
27#include <cstdint>
28#include <list>
29
30namespace esi {
31class AcceleratorConnection;
32class Engine;
33namespace services {
34class Service;
35}
36
37// While building the design, keep around a std::map of active services indexed
38// by the service name. When a new service is encountered during descent, add it
39// to the table (perhaps overwriting one). Modifications to the table only apply
40// to the current branch, so copy this and update it at each level of the tree.
41using ServiceTable = std::map<std::string, services::Service *>;
42
43namespace services {
44
45/// Add a custom interface to a service client at a particular point in the
46/// design hierarchy.
47class ServicePort : public BundlePort {
48public:
50 virtual ~ServicePort() = default;
51 // Get a description of the service port.
52 virtual std::optional<std::string> toString(bool oneLine = false) const {
53 return std::nullopt;
54 }
55};
56
57/// Parent class of all APIs modeled as 'services'. May or may not map to a
58/// hardware side 'service'.
59class Service {
60public:
61 using Type = const std::type_info &;
63 virtual ~Service() = default;
64
65 virtual std::string getServiceSymbol() const = 0;
66
67 /// Create a "child" service of this service. Does not have to be the same
68 /// service type, but typically is. Used when a service already exists in the
69 /// active services table, but a new one wants to replace it. Useful for cases
70 /// where the child service needs to use the parent service. Defaults to
71 /// calling the `getService` method on `AcceleratorConnection` to get the
72 /// global service, implying that the child service does not need to use the
73 /// service it is replacing.
74 virtual Service *getChildService(Service::Type service, AppIDPath id = {},
75 std::string implName = {},
76 ServiceImplDetails details = {},
77 HWClientDetails clients = {});
78
79 /// Get specialized port for this service to attach to the given appid path.
80 /// Null returns mean nothing to attach.
81 virtual BundlePort *getPort(AppIDPath id, const BundleType *type) const {
82 return nullptr;
83 }
84
86
87protected:
89};
90
91/// A service for which there are no standard services registered. Requires
92/// ports be added to the design hierarchy instead of high level interfaces like
93/// the ones in StdServices.h.
94class CustomService : public Service {
95public:
97 const ServiceImplDetails &details,
98 const HWClientDetails &clients);
99 virtual ~CustomService() = default;
100
101 virtual std::string getServiceSymbol() const override {
102 return serviceSymbol;
103 }
104 virtual BundlePort *getPort(AppIDPath id,
105 const BundleType *type) const override;
106
107protected:
108 std::string serviceSymbol;
110};
111
112/// Information about the Accelerator system.
113class SysInfo : public Service {
114public:
115 using Service::Service;
116 virtual ~SysInfo() = default;
117
118 virtual std::string getServiceSymbol() const override;
119
120 /// Get the ESI version number to check version compatibility.
121 virtual uint32_t getEsiVersion() const = 0;
122
123 /// Get the current cycle count of the accelerator system.
124 virtual std::optional<uint64_t> getCycleCount() const { return std::nullopt; }
125 /// Get the "core" clock frequency of the accelerator system in Hz. Returns
126 /// nullopt if the accelerator does not provide this information.
127 virtual std::optional<uint64_t> getCoreClockFrequency() const {
128 return std::nullopt;
129 }
130
131 /// Return the JSON-formatted system manifest.
132 virtual std::string getJsonManifest() const;
133
134 /// Return the zlib compressed JSON system manifest.
135 virtual std::vector<uint8_t> getCompressedManifest() const = 0;
136};
137
138class MMIO : public Service {
139public:
140 static constexpr std::string_view StdName = "esi.service.std.mmio";
141
142 /// Describe a region (slice) of MMIO space.
144 uint32_t base;
145 uint32_t size;
146 };
147
148 MMIO(AcceleratorConnection &, const AppIDPath &idPath,
149 const HWClientDetails &clients);
150 virtual ~MMIO() = default;
151
152 /// Read a 64-bit value from the global MMIO space.
153 virtual uint64_t read(uint32_t addr) const = 0;
154 /// Write a 64-bit value to the global MMIO space.
155 virtual void write(uint32_t addr, uint64_t data) = 0;
156 /// Get the regions of MMIO space that this service manages. Otherwise known
157 /// as the base address table.
158 const std::map<AppIDPath, RegionDescriptor> &getRegions() const {
159 return regions;
160 }
161
162 /// If the service is a MMIO service, return a region of the MMIO space which
163 /// peers into ours.
164 virtual Service *getChildService(Service::Type service, AppIDPath id = {},
165 std::string implName = {},
166 ServiceImplDetails details = {},
167 HWClientDetails clients = {}) override;
168
169 virtual std::string getServiceSymbol() const override;
170
171 /// Get a MMIO region port for a particular region descriptor.
172 virtual BundlePort *getPort(AppIDPath id,
173 const BundleType *type) const override;
174
175private:
176 /// MMIO base address table.
177 std::map<AppIDPath, RegionDescriptor> regions;
178
179public:
180 /// A "slice" of some parent MMIO space.
181 class MMIORegion : public ServicePort {
182 friend class MMIO;
184
185 public:
186 /// Get the offset (and size) of the region in the parent (usually global)
187 /// MMIO address space.
188 virtual RegionDescriptor getDescriptor() const { return desc; };
189 /// Read a 64-bit value from this region, not the global address space.
190 virtual uint64_t read(uint32_t addr) const;
191 /// Write a 64-bit value to this region, not the global address space.
192 virtual void write(uint32_t addr, uint64_t data);
193
194 virtual std::optional<std::string>
195 toString(bool oneLine = false) const override {
196 return "MMIO region " + toHex(desc.base) + " - " +
198 }
199
200 private:
203 };
204};
205
206/// Implement the SysInfo API for a standard MMIO protocol.
207class MMIOSysInfo final : public SysInfo {
208public:
209 MMIOSysInfo(const MMIO *);
210
211 /// Get the ESI version number to check version compatibility.
212 uint32_t getEsiVersion() const override;
213
214 /// Get the current cycle count of the accelerator system's core clock.
215 std::optional<uint64_t> getCycleCount() const override;
216 /// Get the "core" clock frequency of the accelerator system in Hz. Returns
217 /// nullopt if the accelerator does not provide this information.
218 std::optional<uint64_t> getCoreClockFrequency() const override;
219
220 /// Return the zlib compressed JSON system manifest.
221 virtual std::vector<uint8_t> getCompressedManifest() const override;
222
223private:
224 const MMIO *mmio;
225};
226
227class HostMem : public Service {
228public:
229 static constexpr std::string_view StdName = "esi.service.std.hostmem";
230
231 using Service::Service;
232 virtual ~HostMem() = default;
233 virtual std::string getServiceSymbol() const override;
234
235 /// RAII memory region for host memory. Automatically frees the memory when
236 /// deconstructed.
238 virtual ~HostMemRegion() = default;
239 /// Get a pointer to the host memory.
240 virtual void *getPtr() const = 0;
241 /// Sometimes the pointer the device sees is different from the pointer the
242 /// host sees. Call this functon to get the device pointer.
243 virtual void *getDevicePtr() const { return getPtr(); }
244 operator void *() const { return getPtr(); }
245 virtual std::size_t getSize() const = 0;
246 /// Flush the memory region to ensure that the device sees the latest
247 /// contents. Because some platforms require it before DMA transactions, it
248 /// is recommended to call this before any DMA on all platforms. On
249 /// platforms which don't require it, it is a cheap no-op virtual method
250 /// call.
251 virtual void flush() {}
252 };
253
254 /// Options for allocating host memory.
255 struct Options {
256 bool writeable = false;
257 bool useLargePages = false;
258 };
259
260 /// In cases where necessary, enable host memory services.
261 virtual void start() {}
262
263 /// Allocate a region of host memory in accelerator accessible address space.
264 virtual std::unique_ptr<HostMemRegion> allocate(std::size_t size,
265 Options opts) const = 0;
266
267 /// Try to make a region of host memory accessible to the accelerator. Returns
268 /// 'false' on failure. It is optional for an accelerator backend to implement
269 /// this, so client code needs to have a fallback for when this returns
270 /// 'false'. On success, it is the client's responsibility to ensure that the
271 /// memory eventually gets unmapped.
272 virtual bool mapMemory(void *ptr, std::size_t size, Options opts) const {
273 return false;
274 }
275 /// Unmap memory which was previously mapped with 'mapMemory'. Undefined
276 /// behavior when called with a pointer which was not previously mapped.
277 virtual void unmapMemory(void *ptr) const {}
278};
279
280/// Service for calling functions.
281class FuncService : public Service {
282public:
284 HWClientDetails clients);
285
286 virtual std::string getServiceSymbol() const override;
287 virtual BundlePort *getPort(AppIDPath id,
288 const BundleType *type) const override;
289
290 /// A function call which gets attached to a service port.
291 class Function : public ServicePort {
292 friend class FuncService;
293 using ServicePort::ServicePort;
294
295 public:
298
299 void connect();
300 std::future<MessageData> call(const MessageData &arg);
301
302 const esi::Type *getArgType() const {
303 return dynamic_cast<const ChannelType *>(type->findChannel("arg").first)
304 ->getInner();
305 }
306
307 const esi::Type *getResultType() const {
308 return dynamic_cast<const ChannelType *>(
309 type->findChannel("result").first)
310 ->getInner();
311 }
312
313 virtual std::optional<std::string>
314 toString(bool oneLine = false) const override {
315 const esi::Type *argType = getArgType();
316 const esi::Type *resultType = getResultType();
317 return "function " + resultType->toString(oneLine) + "(" +
318 argType->toString(oneLine) + ")";
319 }
320
321 private:
322 std::mutex callMutex;
325 bool connected = false;
326 };
327
328private:
329 std::string symbol;
330};
331
332/// Service for servicing function calls from the accelerator.
333class CallService : public Service {
334public:
336 ServiceImplDetails details);
337
338 virtual std::string getServiceSymbol() const override;
339 virtual BundlePort *getPort(AppIDPath id,
340 const BundleType *type) const override;
341
342 /// A function call which gets attached to a service port.
343 class Callback : public ServicePort {
344 friend class CallService;
347
348 public:
352
353 /// Connect a callback to code which will be executed when the accelerator
354 /// invokes the callback. The 'quick' flag indicates that the callback is
355 /// sufficiently fast that it could be called in the same thread as the
356 /// port callback.
357 void connect(std::function<MessageData(const MessageData &)> callback,
358 bool quick = false);
359
360 const esi::Type *getArgType() const {
361 return dynamic_cast<const ChannelType *>(type->findChannel("arg").first)
362 ->getInner();
363 }
364
365 const esi::Type *getResultType() const {
366 return dynamic_cast<const ChannelType *>(
367 type->findChannel("result").first)
368 ->getInner();
369 }
370
371 virtual std::optional<std::string>
372 toString(bool oneLine = false) const override {
373 const esi::Type *argType = getArgType();
374 const esi::Type *resultType = getResultType();
375 return "callback " + resultType->toString(oneLine) + "(" +
376 argType->toString(oneLine) + ")";
377 }
378
379 private:
383 };
384
385private:
386 std::string symbol;
387};
388
389/// Service for retrieving telemetry data from the accelerator.
390class TelemetryService : public Service {
391public:
392 static constexpr std::string_view StdName = "esi.service.std.telemetry";
393
395 ServiceImplDetails details, HWClientDetails clients);
396
397 virtual std::string getServiceSymbol() const override;
398 virtual BundlePort *getPort(AppIDPath id,
399 const BundleType *type) const override;
400 virtual Service *getChildService(Service::Type service, AppIDPath id = {},
401 std::string implName = {},
402 ServiceImplDetails details = {},
403 HWClientDetails clients = {}) override;
404 MMIO::MMIORegion *getMMIORegion() const;
405
406 /// A telemetry port which gets attached to a service port.
407 class Metric : public ServicePort {
408 friend class TelemetryService;
411 std::optional<uint64_t> offset);
412
413 public:
414 void connect();
415 std::future<MessageData> read();
416 uint64_t readInt();
417
418 virtual std::optional<std::string>
419 toString(bool oneLine = false) const override {
420 const esi::Type *dataType =
421 dynamic_cast<const ChannelType *>(type->findChannel("data").first)
422 ->getInner();
423 return "telemetry " + dataType->toString(oneLine);
424 }
425
426 private:
429 std::optional<uint64_t> offset;
430 };
431
432 std::map<AppIDPath, Metric *> getTelemetryPorts() {
433 std::map<AppIDPath, Metric *> ports;
434 getTelemetryPorts(ports);
435 return ports;
436 }
437 void getTelemetryPorts(std::map<AppIDPath, Metric *> &ports);
438
439private:
442 std::map<AppIDPath, uint64_t> portAddressAssignments;
443 mutable std::map<AppIDPath, Metric *> telemetryPorts;
444 std::list<TelemetryService *> children;
445};
446
447/// Registry of services which can be instantiated directly by the Accelerator
448/// class if the backend doesn't do anything special with a service.
450public:
451 /// Create a service instance from the given details. Returns nullptr if
452 /// 'svcType' isn't registered.
454 Service::Type svcType, AppIDPath id,
455 std::string implName,
456 ServiceImplDetails details,
457 HWClientDetails clients);
458
459 /// Resolve a service type from a string. If the string isn't recognized,
460 /// default to CustomService.
461 static Service::Type lookupServiceType(const std::string &);
462};
463
464} // namespace services
465} // namespace esi
466
467#endif // ESI_RUNTIME_SERVICES_H
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:89
Services provide connections to 'bundles' – collections of named, unidirectional communication channe...
Definition Ports.h:433
PortMap channels
Definition Ports.h:477
const BundleType * type
Definition Ports.h:476
BundlePort(AppID id, const BundleType *type, PortMap channels)
Construct a port.
Definition Ports.cpp:39
Bundles represent a collection of channels.
Definition Types.h:97
std::pair< const Type *, Direction > findChannel(std::string name) const
Definition Types.cpp:108
Channels are the basic communication primitives.
Definition Types.h:118
A logical chunk of data representing serialized data.
Definition Common.h:113
A ChannelPort which reads data from the accelerator.
Definition Ports.h:318
Root class of the ESI type system.
Definition Types.h:34
std::string toString(bool oneLine=false) const
Definition Types.cpp:101
A ChannelPort which sends data to the accelerator.
Definition Ports.h:206
A function call which gets attached to a service port.
Definition Services.h:343
static Callback * get(AcceleratorConnection &acc, AppID id, const BundleType *type, WriteChannelPort &result, ReadChannelPort &arg)
Definition Services.cpp:266
const esi::Type * getArgType() const
Definition Services.h:360
const esi::Type * getResultType() const
Definition Services.h:365
void connect(std::function< MessageData(const MessageData &)> callback, bool quick=false)
Connect a callback to code which will be executed when the accelerator invokes the callback.
Definition Services.cpp:274
virtual std::optional< std::string > toString(bool oneLine=false) const override
Definition Services.h:372
AcceleratorConnection & acc
Definition Services.h:382
Service for servicing function calls from the accelerator.
Definition Services.h:333
virtual std::string getServiceSymbol() const override
Definition Services.cpp:255
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get specialized port for this service to attach to the given appid path.
Definition Services.cpp:257
A service for which there are no standard services registered.
Definition Services.h:94
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get specialized port for this service to attach to the given appid path.
Definition Services.cpp:197
virtual ~CustomService()=default
virtual std::string getServiceSymbol() const override
Definition Services.h:101
A function call which gets attached to a service port.
Definition Services.h:291
const esi::Type * getArgType() const
Definition Services.h:302
const esi::Type * getResultType() const
Definition Services.h:307
std::future< MessageData > call(const MessageData &arg)
Definition Services.cpp:239
virtual std::optional< std::string > toString(bool oneLine=false) const override
Definition Services.h:314
static Function * get(AppID id, BundleType *type, WriteChannelPort &arg, ReadChannelPort &result)
Definition Services.cpp:218
Service for calling functions.
Definition Services.h:281
virtual std::string getServiceSymbol() const override
Definition Services.cpp:211
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get specialized port for this service to attach to the given appid path.
Definition Services.cpp:213
virtual ~HostMem()=default
virtual std::string getServiceSymbol() const override
Definition Services.cpp:184
virtual void start()
In cases where necessary, enable host memory services.
Definition Services.h:261
static constexpr std::string_view StdName
Definition Services.h:229
virtual std::unique_ptr< HostMemRegion > allocate(std::size_t size, Options opts) const =0
Allocate a region of host memory in accelerator accessible address space.
virtual void unmapMemory(void *ptr) const
Unmap memory which was previously mapped with 'mapMemory'.
Definition Services.h:277
virtual bool mapMemory(void *ptr, std::size_t size, Options opts) const
Try to make a region of host memory accessible to the accelerator.
Definition Services.h:272
Implement the SysInfo API for a standard MMIO protocol.
Definition Services.h:207
std::optional< uint64_t > getCycleCount() const override
Get the current cycle count of the accelerator system's core clock.
Definition Services.cpp:153
virtual std::vector< uint8_t > getCompressedManifest() const override
Return the zlib compressed JSON system manifest.
Definition Services.cpp:164
std::optional< uint64_t > getCoreClockFrequency() const override
Get the "core" clock frequency of the accelerator system in Hz.
Definition Services.cpp:157
uint32_t getEsiVersion() const override
Get the ESI version number to check version compatibility.
Definition Services.cpp:146
A "slice" of some parent MMIO space.
Definition Services.h:181
virtual uint64_t read(uint32_t addr) const
Read a 64-bit value from this region, not the global address space.
Definition Services.cpp:132
virtual std::optional< std::string > toString(bool oneLine=false) const override
Definition Services.h:195
virtual void write(uint32_t addr, uint64_t data)
Write a 64-bit value to this region, not the global address space.
Definition Services.cpp:137
virtual RegionDescriptor getDescriptor() const
Get the offset (and size) of the region in the parent (usually global) MMIO address space.
Definition Services.h:188
virtual uint64_t read(uint32_t addr) const =0
Read a 64-bit value from the global MMIO space.
virtual ~MMIO()=default
virtual void write(uint32_t addr, uint64_t data)=0
Write a 64-bit value to the global MMIO space.
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get a MMIO region port for a particular region descriptor.
Definition Services.cpp:94
std::map< AppIDPath, RegionDescriptor > regions
MMIO base address table.
Definition Services.h:177
static constexpr std::string_view StdName
Definition Services.h:140
virtual Service * getChildService(Service::Type service, AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={}) override
If the service is a MMIO service, return a region of the MMIO space which peers into ours.
Definition Services.cpp:118
const std::map< AppIDPath, RegionDescriptor > & getRegions() const
Get the regions of MMIO space that this service manages.
Definition Services.h:158
virtual std::string getServiceSymbol() const override
Definition Services.cpp:91
Add a custom interface to a service client at a particular point in the design hierarchy.
Definition Services.h:47
virtual ~ServicePort()=default
virtual std::optional< std::string > toString(bool oneLine=false) const
Definition Services.h:52
Registry of services which can be instantiated directly by the Accelerator class if the backend doesn...
Definition Services.h:449
static Service::Type lookupServiceType(const std::string &)
Resolve a service type from a string.
Definition Services.cpp:434
static Service * createService(AcceleratorConnection *acc, Service::Type svcType, AppIDPath id, std::string implName, ServiceImplDetails details, HWClientDetails clients)
Create a service instance from the given details.
Definition Services.cpp:417
Parent class of all APIs modeled as 'services'.
Definition Services.h:59
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const
Get specialized port for this service to attach to the given appid path.
Definition Services.h:81
virtual std::string getServiceSymbol() const =0
AcceleratorConnection & getConnection() const
Definition Services.h:85
const std::type_info & Type
Definition Services.h:61
virtual Service * getChildService(Service::Type service, AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={})
Create a "child" service of this service.
Definition Services.cpp:28
Service(AcceleratorConnection &conn)
Definition Services.h:62
virtual ~Service()=default
AcceleratorConnection & conn
Definition Services.h:88
Information about the Accelerator system.
Definition Services.h:113
virtual std::optional< uint64_t > getCoreClockFrequency() const
Get the "core" clock frequency of the accelerator system in Hz.
Definition Services.h:127
virtual std::string getJsonManifest() const
Return the JSON-formatted system manifest.
Definition Services.cpp:40
virtual uint32_t getEsiVersion() const =0
Get the ESI version number to check version compatibility.
virtual std::vector< uint8_t > getCompressedManifest() const =0
Return the zlib compressed JSON system manifest.
virtual std::string getServiceSymbol() const override
Definition Services.cpp:35
virtual std::optional< uint64_t > getCycleCount() const
Get the current cycle count of the accelerator system.
Definition Services.h:124
virtual ~SysInfo()=default
A telemetry port which gets attached to a service port.
Definition Services.h:407
std::optional< uint64_t > offset
Definition Services.h:429
virtual std::optional< std::string > toString(bool oneLine=false) const override
Definition Services.h:419
void connect()
Connect to a particular telemetry port. Offset should be non-nullopt.
Definition Services.cpp:389
std::future< MessageData > read()
Definition Services.cpp:396
const TelemetryService * telemetryService
Definition Services.h:427
Service for retrieving telemetry data from the accelerator.
Definition Services.h:390
std::list< TelemetryService * > children
Definition Services.h:444
std::map< AppIDPath, Metric * > getTelemetryPorts()
Definition Services.h:432
MMIO::MMIORegion * mmio
Definition Services.h:441
MMIO::MMIORegion * getMMIORegion() const
Definition Services.cpp:342
std::map< AppIDPath, Metric * > telemetryPorts
Definition Services.h:443
std::map< AppIDPath, uint64_t > portAddressAssignments
Definition Services.h:442
static constexpr std::string_view StdName
Definition Services.h:392
virtual std::string getServiceSymbol() const override
Definition Services.cpp:338
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get specialized port for this service to attach to the given appid path.
Definition Services.cpp:361
virtual Service * getChildService(Service::Type service, AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={}) override
Create a "child" service of this service.
Definition Services.cpp:372
Definition esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition Common.h:108
std::string toHex(void *val)
Definition Common.cpp:37
std::map< std::string, ChannelPort & > PortMap
Definition Ports.h:29
std::map< std::string, services::Service * > ServiceTable
Definition Services.h:41
std::vector< HWClientDetail > HWClientDetails
Definition Common.h:107
RAII memory region for host memory.
Definition Services.h:237
virtual void * getDevicePtr() const
Sometimes the pointer the device sees is different from the pointer the host sees.
Definition Services.h:243
virtual void * getPtr() const =0
Get a pointer to the host memory.
virtual void flush()
Flush the memory region to ensure that the device sees the latest contents.
Definition Services.h:251
virtual std::size_t getSize() const =0
Options for allocating host memory.
Definition Services.h:255
Describe a region (slice) of MMIO space.
Definition Services.h:143