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 /// Return the JSON-formatted system manifest.
124 virtual std::string getJsonManifest() const;
125
126 /// Return the zlib compressed JSON system manifest.
127 virtual std::vector<uint8_t> getCompressedManifest() const = 0;
128};
129
130class MMIO : public Service {
131public:
132 static constexpr std::string_view StdName = "esi.service.std.mmio";
133
134 /// Describe a region (slice) of MMIO space.
136 uint32_t base;
137 uint32_t size;
138 };
139
140 MMIO(AcceleratorConnection &, const AppIDPath &idPath,
141 const HWClientDetails &clients);
142 virtual ~MMIO() = default;
143
144 /// Read a 64-bit value from the global MMIO space.
145 virtual uint64_t read(uint32_t addr) const = 0;
146 /// Write a 64-bit value to the global MMIO space.
147 virtual void write(uint32_t addr, uint64_t data) = 0;
148 /// Get the regions of MMIO space that this service manages. Otherwise known
149 /// as the base address table.
150 const std::map<AppIDPath, RegionDescriptor> &getRegions() const {
151 return regions;
152 }
153
154 /// If the service is a MMIO service, return a region of the MMIO space which
155 /// peers into ours.
156 virtual Service *getChildService(Service::Type service, AppIDPath id = {},
157 std::string implName = {},
158 ServiceImplDetails details = {},
159 HWClientDetails clients = {}) override;
160
161 virtual std::string getServiceSymbol() const override;
162
163 /// Get a MMIO region port for a particular region descriptor.
164 virtual BundlePort *getPort(AppIDPath id,
165 const BundleType *type) const override;
166
167private:
168 /// MMIO base address table.
169 std::map<AppIDPath, RegionDescriptor> regions;
170
171public:
172 /// A "slice" of some parent MMIO space.
173 class MMIORegion : public ServicePort {
174 friend class MMIO;
176
177 public:
178 /// Get the offset (and size) of the region in the parent (usually global)
179 /// MMIO address space.
180 virtual RegionDescriptor getDescriptor() const { return desc; };
181 /// Read a 64-bit value from this region, not the global address space.
182 virtual uint64_t read(uint32_t addr) const;
183 /// Write a 64-bit value to this region, not the global address space.
184 virtual void write(uint32_t addr, uint64_t data);
185
186 virtual std::optional<std::string>
187 toString(bool oneLine = false) const override {
188 return "MMIO region " + toHex(desc.base) + " - " +
190 }
191
192 private:
195 };
196};
197
198/// Implement the SysInfo API for a standard MMIO protocol.
199class MMIOSysInfo final : public SysInfo {
200public:
201 MMIOSysInfo(const MMIO *);
202
203 /// Get the ESI version number to check version compatibility.
204 uint32_t getEsiVersion() const override;
205
206 /// Return the zlib compressed JSON system manifest.
207 virtual std::vector<uint8_t> getCompressedManifest() const override;
208
209private:
210 const MMIO *mmio;
211};
212
213class HostMem : public Service {
214public:
215 static constexpr std::string_view StdName = "esi.service.std.hostmem";
216
217 using Service::Service;
218 virtual ~HostMem() = default;
219 virtual std::string getServiceSymbol() const override;
220
221 /// RAII memory region for host memory. Automatically frees the memory when
222 /// deconstructed.
224 virtual ~HostMemRegion() = default;
225 /// Get a pointer to the host memory.
226 virtual void *getPtr() const = 0;
227 /// Sometimes the pointer the device sees is different from the pointer the
228 /// host sees. Call this functon to get the device pointer.
229 virtual void *getDevicePtr() const { return getPtr(); }
230 operator void *() const { return getPtr(); }
231 virtual std::size_t getSize() const = 0;
232 /// Flush the memory region to ensure that the device sees the latest
233 /// contents. Because some platforms require it before DMA transactions, it
234 /// is recommended to call this before any DMA on all platforms. On
235 /// platforms which don't require it, it is a cheap no-op virtual method
236 /// call.
237 virtual void flush() {}
238 };
239
240 /// Options for allocating host memory.
241 struct Options {
242 bool writeable = false;
243 bool useLargePages = false;
244 };
245
246 /// In cases where necessary, enable host memory services.
247 virtual void start() {}
248
249 /// Allocate a region of host memory in accelerator accessible address space.
250 virtual std::unique_ptr<HostMemRegion> allocate(std::size_t size,
251 Options opts) const = 0;
252
253 /// Try to make a region of host memory accessible to the accelerator. Returns
254 /// 'false' on failure. It is optional for an accelerator backend to implement
255 /// this, so client code needs to have a fallback for when this returns
256 /// 'false'. On success, it is the client's responsibility to ensure that the
257 /// memory eventually gets unmapped.
258 virtual bool mapMemory(void *ptr, std::size_t size, Options opts) const {
259 return false;
260 }
261 /// Unmap memory which was previously mapped with 'mapMemory'. Undefined
262 /// behavior when called with a pointer which was not previously mapped.
263 virtual void unmapMemory(void *ptr) const {}
264};
265
266/// Service for calling functions.
267class FuncService : public Service {
268public:
270 HWClientDetails clients);
271
272 virtual std::string getServiceSymbol() const override;
273 virtual BundlePort *getPort(AppIDPath id,
274 const BundleType *type) const override;
275
276 /// A function call which gets attached to a service port.
277 class Function : public ServicePort {
278 friend class FuncService;
279 using ServicePort::ServicePort;
280
281 public:
284
285 void connect();
286 std::future<MessageData> call(const MessageData &arg);
287
288 const esi::Type *getArgType() const {
289 return dynamic_cast<const ChannelType *>(type->findChannel("arg").first)
290 ->getInner();
291 }
292
293 const esi::Type *getResultType() const {
294 return dynamic_cast<const ChannelType *>(
295 type->findChannel("result").first)
296 ->getInner();
297 }
298
299 virtual std::optional<std::string>
300 toString(bool oneLine = false) const override {
301 const esi::Type *argType = getArgType();
302 const esi::Type *resultType = getResultType();
303 return "function " + resultType->toString(oneLine) + "(" +
304 argType->toString(oneLine) + ")";
305 }
306
307 private:
308 std::mutex callMutex;
311 bool connected = false;
312 };
313
314private:
315 std::string symbol;
316};
317
318/// Service for servicing function calls from the accelerator.
319class CallService : public Service {
320public:
322 ServiceImplDetails details);
323
324 virtual std::string getServiceSymbol() const override;
325 virtual BundlePort *getPort(AppIDPath id,
326 const BundleType *type) const override;
327
328 /// A function call which gets attached to a service port.
329 class Callback : public ServicePort {
330 friend class CallService;
333
334 public:
338
339 /// Connect a callback to code which will be executed when the accelerator
340 /// invokes the callback. The 'quick' flag indicates that the callback is
341 /// sufficiently fast that it could be called in the same thread as the
342 /// port callback.
343 void connect(std::function<MessageData(const MessageData &)> callback,
344 bool quick = false);
345
346 const esi::Type *getArgType() const {
347 return dynamic_cast<const ChannelType *>(type->findChannel("arg").first)
348 ->getInner();
349 }
350
351 const esi::Type *getResultType() const {
352 return dynamic_cast<const ChannelType *>(
353 type->findChannel("result").first)
354 ->getInner();
355 }
356
357 virtual std::optional<std::string>
358 toString(bool oneLine = false) const override {
359 const esi::Type *argType = getArgType();
360 const esi::Type *resultType = getResultType();
361 return "callback " + resultType->toString(oneLine) + "(" +
362 argType->toString(oneLine) + ")";
363 }
364
365 private:
369 };
370
371private:
372 std::string symbol;
373};
374
375/// Service for retrieving telemetry data from the accelerator.
376class TelemetryService : public Service {
377public:
378 static constexpr std::string_view StdName = "esi.service.std.telemetry";
379
381 ServiceImplDetails details, HWClientDetails clients);
382
383 virtual std::string getServiceSymbol() const override;
384 virtual BundlePort *getPort(AppIDPath id,
385 const BundleType *type) const override;
386 virtual Service *getChildService(Service::Type service, AppIDPath id = {},
387 std::string implName = {},
388 ServiceImplDetails details = {},
389 HWClientDetails clients = {}) override;
390 MMIO::MMIORegion *getMMIORegion() const;
391
392 /// A telemetry port which gets attached to a service port.
393 class Metric : public ServicePort {
394 friend class TelemetryService;
397 std::optional<uint64_t> offset);
398
399 public:
400 void connect();
401 std::future<MessageData> read();
402 uint64_t readInt();
403
404 virtual std::optional<std::string>
405 toString(bool oneLine = false) const override {
406 const esi::Type *dataType =
407 dynamic_cast<const ChannelType *>(type->findChannel("data").first)
408 ->getInner();
409 return "telemetry " + dataType->toString(oneLine);
410 }
411
412 private:
415 std::optional<uint64_t> offset;
416 };
417
418 std::map<AppIDPath, Metric *> getTelemetryPorts() {
419 std::map<AppIDPath, Metric *> ports;
420 getTelemetryPorts(ports);
421 return ports;
422 }
423 void getTelemetryPorts(std::map<AppIDPath, Metric *> &ports);
424
425private:
428 std::map<AppIDPath, uint64_t> portAddressAssignments;
429 mutable std::map<AppIDPath, Metric *> telemetryPorts;
430 std::list<TelemetryService *> children;
431};
432
433/// Registry of services which can be instantiated directly by the Accelerator
434/// class if the backend doesn't do anything special with a service.
436public:
437 /// Create a service instance from the given details. Returns nullptr if
438 /// 'svcType' isn't registered.
440 Service::Type svcType, AppIDPath id,
441 std::string implName,
442 ServiceImplDetails details,
443 HWClientDetails clients);
444
445 /// Resolve a service type from a string. If the string isn't recognized,
446 /// default to CustomService.
447 static Service::Type lookupServiceType(const std::string &);
448};
449
450} // namespace services
451} // namespace esi
452
453#endif // ESI_RUNTIME_SERVICES_H
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:79
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:329
static Callback * get(AcceleratorConnection &acc, AppID id, const BundleType *type, WriteChannelPort &result, ReadChannelPort &arg)
Definition Services.cpp:255
const esi::Type * getArgType() const
Definition Services.h:346
const esi::Type * getResultType() const
Definition Services.h:351
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:263
virtual std::optional< std::string > toString(bool oneLine=false) const override
Definition Services.h:358
AcceleratorConnection & acc
Definition Services.h:368
Service for servicing function calls from the accelerator.
Definition Services.h:319
virtual std::string getServiceSymbol() const override
Definition Services.cpp:244
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:246
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:186
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:277
const esi::Type * getArgType() const
Definition Services.h:288
const esi::Type * getResultType() const
Definition Services.h:293
std::future< MessageData > call(const MessageData &arg)
Definition Services.cpp:228
virtual std::optional< std::string > toString(bool oneLine=false) const override
Definition Services.h:300
static Function * get(AppID id, BundleType *type, WriteChannelPort &arg, ReadChannelPort &result)
Definition Services.cpp:207
Service for calling functions.
Definition Services.h:267
virtual std::string getServiceSymbol() const override
Definition Services.cpp:200
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:202
virtual ~HostMem()=default
virtual std::string getServiceSymbol() const override
Definition Services.cpp:173
virtual void start()
In cases where necessary, enable host memory services.
Definition Services.h:247
static constexpr std::string_view StdName
Definition Services.h:215
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:263
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:258
Implement the SysInfo API for a standard MMIO protocol.
Definition Services.h:199
virtual std::vector< uint8_t > getCompressedManifest() const override
Return the zlib compressed JSON system manifest.
Definition Services.cpp:153
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:173
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:187
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:180
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:169
static constexpr std::string_view StdName
Definition Services.h:132
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:150
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:435
static Service::Type lookupServiceType(const std::string &)
Resolve a service type from a string.
Definition Services.cpp:423
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:406
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::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 ~SysInfo()=default
A telemetry port which gets attached to a service port.
Definition Services.h:393
std::optional< uint64_t > offset
Definition Services.h:415
virtual std::optional< std::string > toString(bool oneLine=false) const override
Definition Services.h:405
void connect()
Connect to a particular telemetry port. Offset should be non-nullopt.
Definition Services.cpp:378
std::future< MessageData > read()
Definition Services.cpp:385
const TelemetryService * telemetryService
Definition Services.h:413
Service for retrieving telemetry data from the accelerator.
Definition Services.h:376
std::list< TelemetryService * > children
Definition Services.h:430
std::map< AppIDPath, Metric * > getTelemetryPorts()
Definition Services.h:418
MMIO::MMIORegion * mmio
Definition Services.h:427
MMIO::MMIORegion * getMMIORegion() const
Definition Services.cpp:331
std::map< AppIDPath, Metric * > telemetryPorts
Definition Services.h:429
std::map< AppIDPath, uint64_t > portAddressAssignments
Definition Services.h:428
static constexpr std::string_view StdName
Definition Services.h:378
virtual std::string getServiceSymbol() const override
Definition Services.cpp:327
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:350
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:361
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:223
virtual void * getDevicePtr() const
Sometimes the pointer the device sees is different from the pointer the host sees.
Definition Services.h:229
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:237
virtual std::size_t getSize() const =0
Options for allocating host memory.
Definition Services.h:241
Describe a region (slice) of MMIO space.
Definition Services.h:135