[Profile] Allow multiple calls to get profile data
[platform/adaptation/npu/trix-engine.git] / src / core / ne-profiler.cc
index 98d5f15..20fcb80 100644 (file)
 
 #include "ne-profiler.h"
 
-ModelProfiler::ModelProfiler (const DriverAPI * api)
-  : api_ (api)
-{
-}
+ModelProfiler::ModelProfiler (const DriverAPI *api) : api_ (api) {}
 
-ModelProfiler::~ModelProfiler ()
-{
+ModelProfiler::~ModelProfiler () {
   profile_map_.clear ();
 }
 
 int
-ModelProfiler::appendTask (int task_id, const Model * model)
-{
-  ProfileData * data = new ProfileData (task_id, model);
-  return profile_map_.insert (task_id, data);
+ModelProfiler::appendRequest (int req_id, const Model *model) {
+  ProfileData *data = new ProfileData (req_id, model);
+  return profile_map_.insert (req_id, data);
+}
+
+int
+ModelProfiler::removeRequest (int req_id) {
+  return profile_map_.remove (req_id);
 }
 
 int
-ModelProfiler::getTaskProfile (int task_id, npu_profile *profile)
-{
-  ProfileData * data = profile_map_.find (task_id);
+ModelProfiler::getProfile (int req_id, const npu_profile_opt &opt, npu_profile *profile) {
+  ProfileData *data = profile_map_.find (req_id);
   if (data == nullptr)
     return -ENOENT;
 
-  const Model * model = data->getModel ();
+  const Model *model = data->getModel ();
   if (model == nullptr)
     return -EINVAL;
 
-  int status = api_->getProfile (task_id, profile);
+  int status = api_->getProfile (req_id, profile);
   if (status != 0)
     return status;
 
-  HWmem * extended = model->getExtendedMetadata ();
-  if (extended != nullptr)
-    manipulateProfile (extended, profile);
-
-  profile_map_.remove (task_id);
+  if (opt.level == PROFILE_LEVEL_EXT_META || opt.level == PROFILE_LEVEL_LAYER) {
+    HWmem *extended = model->getExtendedMetadata ();
+    if (extended != nullptr)
+      manipulateProfile (extended, profile);
+    else if (opt.level == PROFILE_LEVEL_LAYER)
+      status = -EINVAL;
+  }
 
-  return 0;
+  return status;
 }
 
 void
-ModelProfiler::manipulateProfile (HWmem * extended, npu_profile *profile)
-{
+ModelProfiler::manipulateProfile (HWmem *extended, npu_profile *profile) {
   npubin_meta_profile *meta_profile =
-    reinterpret_cast <npubin_meta_profile *> (extended->getData ());
-  npu_profile_layer * new_layers =
-    new npu_profile_layer[meta_profile->node_entry_num + 1];
+      reinterpret_cast<npubin_meta_profile *> (extended->getData ());
+  npu_profile_layer *new_layers = new npu_profile_layer[meta_profile->node_entry_num + 1];
 
-  npu_profile_layer * unclassified =
-    &new_layers[meta_profile->node_entry_num];
+  npu_profile_layer *unclassified = &new_layers[meta_profile->node_entry_num];
 
   snprintf (unclassified->name, NPU_OPNAME_MAX - 1, "%s", "Unclassified");
   unclassified->name[NPU_OPNAME_MAX - 1] = '\x00';
@@ -96,14 +94,14 @@ ModelProfiler::manipulateProfile (HWmem * extended, npu_profile *profile)
       std::cerr << "Zero length detected at ";
       std::cerr << id << "th node" << std::endl;
 
-      delete [] new_layers;
+      delete[] new_layers;
       return;
     }
 
     std::string name (meta_profile->entry_data + pos);
     pos += length;
 
-    npu_profile_layer * layer = &new_layers[i];
+    npu_profile_layer *layer = &new_layers[i];
 
     snprintf (layer->name, NPU_OPNAME_MAX - 1, "%s", name.c_str ());
     layer->name[NPU_OPNAME_MAX - 1] = '\x00';
@@ -117,7 +115,7 @@ ModelProfiler::manipulateProfile (HWmem * extended, npu_profile *profile)
     layer->sram_read_bytes = 0;
     layer->sram_write_bytes = 0;
 
-    node_table.insert(std::make_pair(id, layer));
+    node_table.insert (std::make_pair (id, layer));
   }
 
   /** 2) parsing visa table */
@@ -132,14 +130,14 @@ ModelProfiler::manipulateProfile (HWmem * extended, npu_profile *profile)
     pos += sizeof (uint32_t);
 
     if (node_num > 0) {
-      uint32_t * node_ids = (uint32_t *) (meta_profile->entry_data + pos);
+      uint32_t *node_ids = (uint32_t *) (meta_profile->entry_data + pos);
 
       for (uint32_t j = 0; j < node_num; j++) {
         uint32_t node_id = node_ids[j];
         auto it = node_table.find (node_id);
 
         if (it != node_table.end ()) {
-          npu_profile_layer * layer = it->second;
+          npu_profile_layer *layer = it->second;
 
           /** TODO: evenly divided to fused layers */
           layer->running_cycles += profile->layers[i].running_cycles / node_num;
@@ -171,13 +169,11 @@ ModelProfiler::manipulateProfile (HWmem * extended, npu_profile *profile)
   /** 3) profile data mapping */
   size_t num_layers = node_table.size ();
   if (num_layers > 0) {
-    delete [] profile->layers;
+    delete[] profile->layers;
 
     profile->layers = new_layers;
     profile->num_layers = num_layers + 1;
   } else {
-    delete [] new_layers;
+    delete[] new_layers;
   }
 }
-
-