[API] Add new API to check the device status before running
authorDongju Chae <dongju.chae@samsung.com>
Wed, 17 Jun 2020 02:47:06 +0000 (11:47 +0900)
committer채동주/On-Device Lab(SR)/Staff Engineer/삼성전자 <dongju.chae@samsung.com>
Wed, 17 Jun 2020 07:41:05 +0000 (16:41 +0900)
This patch adds new API to check the device status before running.
It's one of VD requirments in July release.

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
include/common/typedef.h
include/host/libnpuhost.h
src/core/ne-handler.cc
src/core/ne-handler.h
src/core/npu/NPUdrvAPI.h

index 8a2ee47..6bf8bdf 100644 (file)
@@ -109,6 +109,15 @@ typedef enum {
 } npu_notimode;
 
 /**
+ * @brief Description of npu device status
+ */
+typedef enum {
+  NPU_ERROR = 0, /**< The NPU is not available for now (check the log file for details) */
+  NPU_READY = 1, /**< The NPU is available but the request can be delayed due to others */
+  NPU_IDLE = 2,  /**< The NPU is available and there's no active request currently */
+} npu_status;
+
+/**
  * @brief Operable modes of NPU when the inputs are from NPU's own hardware.
  * @note this mode will decide which input service performs the inference of a model.
  */
index 477c3bd..7dd8de0 100644 (file)
@@ -285,6 +285,15 @@ int runNPU_async(npudev_h dev, uint32_t modelid, const input_buffers *input,
 int getNPU_memoryStatus(npudev_h dev, size_t *alloc_total, size_t *free_total);
 
 /**
+ * @brief Get the current device status to be used
+ * @param[in] dev The NPU device handle
+ * @param[out] status the device status
+ * @param[out] num_requests the number of running requests (or pending)
+ * @return 0 if no error, otherwise a negative errno.
+ */
+int getNPU_deviceStatus(npudev_h dev, npu_status *status, uint32_t *num_requests);
+
+/**
  * [IMPORTANT] Descriptions for buffer allocation APIs.
  *
  * NPU Engine provides some APIs to allocate model and input buffers for users.
@@ -394,14 +403,10 @@ int runNPU_async_noalloc(npudev_h dev, uint32_t modelid, const input_buffers *in
 /** @todo func prototype TBD */
 int getRegisteredNPUmodels(npudev_h dev, uint32_t **modelids, uint32_t *nummodels);
 
-/** @todo func prototype TBD */
 int waitNPUrun(npudev_h dev);
 
 /** @todo func prototype TBD */
-int getNPUrunStatus(npudev_h dev, uint64_t *status);
-
-/** @todo func prototype TBD */
-int getNPUstatus(npudev_h dev, uint64_t *status);
+int getNPU_runStatus(npudev_h dev, uint64_t *status);
 
 #if defined(__cplusplus)
 }
index 0682587..a762ced 100644 (file)
@@ -289,6 +289,20 @@ int getNPU_memoryStatus(npudev_h dev, size_t *alloc_total, size_t *free_total)
 }
 
 /**
+ * @brief Get the current device status to be used
+ * @param[in] dev The NPU device handle
+ * @param[out] status the device status
+ * @param[out] num_requests the number of running requests (or pending)
+ * @return 0 if no error, otherwise a negative errno.
+ */
+int getNPU_deviceStatus(npudev_h dev, npu_status *status, uint32_t *num_requests)
+{
+  INIT_HOST_HANDLER (host_handler, dev);
+
+  return host_handler->getDeviceStatus (status, num_requests);
+}
+
+/**
  * @brief Get metadata for NPU model
  * @param[in] model The path of model binary file
  * @param[in] need_extra whether you want to extract the extra data in metadata
@@ -758,6 +772,34 @@ HostHandler::getMemoryStatus (size_t *alloc_total, size_t *free_total)
   return api->getMemoryStatus (alloc_total, free_total);
 }
 
+/**
+ * @brief Get the current device status to be used
+ * @param[out] status the device status
+ * @param[out] num_requests the number of running requests (or pending)
+ * @return 0 if no error, otherwise a negative errno.
+ */
+int
+HostHandler::getDeviceStatus (npu_status *status, uint32_t *num_requests)
+{
+  /** API is always set in initialize () */
+  const DriverAPI * api = device_->getDriverAPI ();
+  assert (api != nullptr);
+
+  device_state_t state = api->isReady ();
+  if (state == device_state_t::STATE_READY) {
+    *num_requests = api->numRequests ();
+    if (*num_requests > 0)
+      *status = NPU_READY;
+    else
+      *status = NPU_IDLE;
+  } else {
+    *num_requests = 0;
+    *status = NPU_ERROR;
+  }
+
+  return 0;
+}
+
 /** implement methods of Device class */
 
 /** @brief constructor of device */
index 69f112f..3970d30 100644 (file)
@@ -54,6 +54,7 @@ class HostHandler {
 
     /** @brief get statistics */
     int getMemoryStatus (size_t *alloc_total, size_t *free_total);
+    int getDeviceStatus (npu_status *status, uint32_t *num_requests);
 
     static int getNumDevices (dev_type type);
     static int getDevice (npudev_h *dev, dev_type type, uint32_t id);
index 371e635..0ab60b4 100644 (file)
@@ -72,6 +72,9 @@ class DriverAPI {
     /** @brief check whether the device is ready (true) or busy (false) */
     virtual device_state_t isReady () const { return device_state_t::STATE_UNKNOWN; }
 
+    /** @brief return a number of requests submitted */
+    virtual uint32_t numRequests () const { return 0; }
+
     /** @brief allocate memory with the given size. return dmabuf fd */
     virtual int alloc (size_t size) const { return -EPERM; }
     /** @brief deallocate memory with the corresponding dmabuf fd */