cbalint13 commented on code in PR #19830:
URL: https://github.com/apache/tvm/pull/19830#discussion_r3435408445


##########
apps/cpp_rpc/rpc_tracker.cc:
##########
@@ -0,0 +1,523 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file rpc_tracker.cc
+ * \brief RPC Tracker implementation.
+ */
+#include "../../apps/cpp_rpc/rpc_tracker.h"
+
+#include <tvm/ffi/function.h>
+#include <tvm/ffi/reflection/registry.h>
+
+#include <algorithm>
+#include <any>
+#include <array>
+#include <condition_variable>
+#include <functional>
+#include <future>
+#include <map>
+#include <memory>
+#include <mutex>
+#include <queue>
+#include <set>
+#include <string>
+#include <thread>
+#include <unordered_map>
+#include <utility>
+#include <vector>
+
+#include "../../apps/cpp_rpc/rpc_tracker_server.h"
+
+namespace tvm {
+namespace runtime {
+
+static std::string setToString(const std::set<std::string>& inputSet) {
+  std::string result = "[";
+  bool first = true;
+  for (const auto& str : inputSet) {
+    if (!first) result += ", ";
+    result += "\"" + str + "\"";
+    first = false;
+  }
+
+  return result + "]";
+}
+
+static std::string stripQuotes(const std::string& Str, char quote = '"') {
+  if (Str.size() >= 2 && Str[0] == quote && Str[Str.size() - 1] == quote) {
+    return Str.substr(1, Str.size() - 2);
+  }
+
+  return Str;
+}
+
+static std::vector<std::string> ParseArray(std::string Raw) {
+  std::vector<std::string> elements;
+
+  if (Raw.size() >= 2 && Raw.front() == '[' && Raw.back() == ']') {
+    Raw = Raw.substr(1, Raw.size() - 2);
+  }
+
+  std::string current;
+  int brace_level = 0;
+  int bracket_level = 0;
+  bool in_quotes = false;
+
+  for (size_t i = 0; i < Raw.size(); ++i) {
+    char c = Raw[i];
+
+    if (c == '"') {
+      in_quotes = !in_quotes;
+    }
+
+    if (!in_quotes) {
+      if (c == '[')
+        bracket_level++;
+      else if (c == ']')
+        bracket_level--;
+      else if (c == '{')
+        brace_level++;
+      else if (c == '}')
+        brace_level--;
+
+      if (c == ',' && bracket_level == 0 && brace_level == 0) {
+        if (!current.empty()) elements.push_back(current);
+        current.clear();
+        if (i + 1 < Raw.size() && Raw[i + 1] == ' ') i++;
+        continue;
+      }
+    }
+    current += c;
+  }
+
+  if (!current.empty()) elements.push_back(current);
+
+  return elements;
+}
+
+static std::map<std::string, std::string> ParseJSONDict(std::string input) {
+  std::map<std::string, std::string> result;
+  size_t pos = 0;
+
+  while ((pos = input.find("\"", pos)) != std::string::npos) {
+    size_t key_start = ++pos;
+    size_t key_end = input.find("\"", key_start);
+    if (key_end == std::string::npos) break;
+
+    std::string key = input.substr(key_start, key_end - key_start);
+    pos = key_end + 1;
+
+    pos = input.find(':', pos);
+    if (pos == std::string::npos) break;
+    pos++;
+
+    while (pos < input.length() && (input[pos] == ' ' || input[pos] == '\t' || 
input[pos] == '\n'))
+      pos++;
+
+    size_t val_start = pos;
+    size_t val_end = pos;
+
+    if (input[pos] == '"') {
+      val_start++;
+      val_end = input.find('"', val_start);
+      pos = val_end + 1;
+    } else if (input[pos] == '[') {
+      int depth = 1;
+      val_end = val_start + 1;
+      while (val_end < input.length() && depth > 0) {
+        if (input[val_end] == '[')
+          depth++;
+        else if (input[val_end] == ']')
+          depth--;
+        val_end++;
+      }
+      pos = val_end;
+    } else {
+      val_end = input.find_first_of(",}", val_start);
+      pos = val_end;
+    }
+
+    if (val_end != std::string::npos) {
+      result[key] = input.substr(val_start, val_end - val_start);
+    }
+  }
+
+  return result;
+}
+
+PriorityScheduler::PriorityScheduler(const std::string& key) : _key(key), 
_request_cnt(0) {}
+
+void PriorityScheduler::_schedule() {
+  std::lock_guard<std::mutex> lock(_lock);
+  while (!_requests.empty() && !_values.empty()) {
+    Request item = _requests.top();
+    ResourceData value = _values.front();
+    if (item.callback(value)) {
+      _values.erase(_values.begin());
+      _requests.pop();
+    } else {
+      break;
+    }
+  }
+}

Review Comment:
   Callback will throw error (exiting the program) so it will not fail here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to