martinzink commented on a change in pull request #1032:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1032#discussion_r621969862



##########
File path: libminifi/src/utils/SystemCPUUsageTracker.cpp
##########
@@ -0,0 +1,190 @@
+/**
+ * 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.
+ */
+
+#include "utils/SystemCPUUsageTracker.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+#ifdef __linux__
+
+SystemCPUUsageTracker::SystemCPUUsageTracker() :
+    total_user_(0), previous_total_user_(0),
+    total_user_low_(0), previous_total_user_low_(0),
+    total_sys_(0), previous_total_sys_(0),
+    total_idle_(0), previous_total_idle_(0) {
+  queryCPUTimes();
+}
+
+double SystemCPUUsageTracker::getCPUUsageAndRestartCollection() {
+  queryCPUTimes();
+  if (isCurrentQuerySameAsPrevious() || isCurrentQueryOlderThanPrevious()) {
+    return -1.0;
+  } else {
+    return getCPUUsageBetweenLastTwoQueries();
+  }
+}
+
+void SystemCPUUsageTracker::queryCPUTimes() {
+  previous_total_user_ = total_user_;
+  previous_total_user_low_ = total_user_low_;
+  previous_total_sys_ = total_sys_;
+  previous_total_idle_ = total_idle_;
+  FILE* file = fopen("/proc/stat", "r");
+  if (fscanf(file, "cpu %lu %lu %lu %lu", &total_user_, &total_user_low_, 
&total_sys_, &total_idle_) != 4) {
+    total_user_ = previous_total_user_;
+    total_user_low_ = previous_total_user_low_;
+    total_idle_ = previous_total_idle_;
+    total_sys_ = previous_total_sys_;
+  }
+  fclose(file);
+}
+
+bool SystemCPUUsageTracker::isCurrentQueryOlderThanPrevious() const {
+  return (total_user_ < previous_total_user_ ||
+          total_user_low_ < previous_total_user_low_ ||
+          total_sys_ < previous_total_sys_ ||
+          total_idle_ < previous_total_idle_);
+}
+
+bool SystemCPUUsageTracker::isCurrentQuerySameAsPrevious() const {
+  return (total_user_ == previous_total_user_ &&
+          total_user_low_ == previous_total_user_low_ &&
+          total_sys_ == previous_total_sys_ &&
+          total_idle_ == previous_total_idle_);
+}
+
+double SystemCPUUsageTracker::getCPUUsageBetweenLastTwoQueries() const {
+  uint64_t total_user_diff = total_user_ - previous_total_user_;
+  uint64_t total_user_low_diff = total_user_low_ - previous_total_user_low_;
+  uint64_t total_system_diff = total_sys_ - previous_total_sys_;
+  uint64_t total_idle_diff = total_idle_ - previous_total_idle_;
+  uint64_t total_diff =  total_user_diff + total_user_low_diff + 
total_system_diff;
+  double percent = 
static_cast<double>(total_diff)/static_cast<double>(total_diff+total_idle_diff);

Review comment:
       That wont happened because this function (protected) is only called 
after the various checks (isCurrentQueryOlderThanPrevious and 
isCurrentQuerySameAsPrevious) which make sure that there are valid differences 
between the last and current query.

##########
File path: libminifi/src/utils/SystemCPUUsageTracker.cpp
##########
@@ -0,0 +1,190 @@
+/**
+ * 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.
+ */
+
+#include "utils/SystemCPUUsageTracker.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+#ifdef __linux__
+
+SystemCPUUsageTracker::SystemCPUUsageTracker() :
+    total_user_(0), previous_total_user_(0),
+    total_user_low_(0), previous_total_user_low_(0),
+    total_sys_(0), previous_total_sys_(0),
+    total_idle_(0), previous_total_idle_(0) {
+  queryCPUTimes();
+}
+
+double SystemCPUUsageTracker::getCPUUsageAndRestartCollection() {
+  queryCPUTimes();
+  if (isCurrentQuerySameAsPrevious() || isCurrentQueryOlderThanPrevious()) {
+    return -1.0;
+  } else {
+    return getCPUUsageBetweenLastTwoQueries();
+  }
+}
+
+void SystemCPUUsageTracker::queryCPUTimes() {

Review comment:
       Yeah, I see your point, changed it in 
https://github.com/apache/nifi-minifi-cpp/pull/1065

##########
File path: libminifi/test/unit/ResponseNodeValueTests.cpp
##########
@@ -0,0 +1,187 @@
+/**
+ *
+ * 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.
+ */
+#include <memory>
+
+#include "../../include/core/state/Value.h"
+#include "../TestBase.h"
+
+template <class T>
+bool canConvertToType(org::apache::nifi::minifi::state::response::ValueNode 
value_node) {
+  T conversion_target;
+  return value_node.getValue()->convertValue(conversion_target);
+}
+
+template <class T>
+bool canConvertToType(org::apache::nifi::minifi::state::response::ValueNode 
value_node, const T& expected_result) {
+  T conversion_target;
+  bool canConvert = value_node.getValue()->convertValue(conversion_target);
+  return canConvert && (expected_result == conversion_target);
+}
+
+TEST_CASE("IntValueNodeTests", "[responsenodevaluetests]") {
+  org::apache::nifi::minifi::state::response::ValueNode value_node;
+
+  int positive_int_value = 6;
+  value_node = positive_int_value;
+  REQUIRE(value_node.getValue()->getTypeIndex() == 
org::apache::nifi::minifi::state::response::Value::INT_TYPE);
+  REQUIRE(canConvertToType<uint32_t>(value_node));
+  REQUIRE(canConvertToType<uint64_t>(value_node));
+  REQUIRE(canConvertToType<int32_t>(value_node));
+  REQUIRE(canConvertToType<int64_t>(value_node));
+  REQUIRE(canConvertToType<int> (value_node));
+  REQUIRE_FALSE(canConvertToType<bool>(value_node));
+  REQUIRE(canConvertToType<double>(value_node));
+  REQUIRE(value_node.to_string() == "6");
+
+  int negative_int_value = -7;
+  value_node = negative_int_value;
+  REQUIRE(value_node.getValue()->getTypeIndex() == 
org::apache::nifi::minifi::state::response::Value::INT_TYPE);
+  REQUIRE_FALSE(canConvertToType<uint32_t>(value_node));
+  REQUIRE_FALSE(canConvertToType<uint64_t>(value_node));
+  REQUIRE(canConvertToType<int32_t>(value_node));
+  REQUIRE(canConvertToType<int64_t>(value_node));
+  REQUIRE(canConvertToType<int> (value_node));
+  REQUIRE_FALSE(canConvertToType<bool>(value_node));
+  REQUIRE(canConvertToType<double>(value_node));
+  REQUIRE(value_node.to_string() == "-7");
+
+  uint32_t max_uint32_value = (uint64_t{1} << 32) - 1;

Review comment:
       changed it in https://github.com/apache/nifi-minifi-cpp/pull/1065

##########
File path: libminifi/test/unit/ResponseNodeValueTests.cpp
##########
@@ -0,0 +1,187 @@
+/**
+ *
+ * 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.
+ */
+#include <memory>
+
+#include "../../include/core/state/Value.h"
+#include "../TestBase.h"
+
+template <class T>
+bool canConvertToType(org::apache::nifi::minifi::state::response::ValueNode 
value_node) {
+  T conversion_target;
+  return value_node.getValue()->convertValue(conversion_target);
+}
+
+template <class T>
+bool canConvertToType(org::apache::nifi::minifi::state::response::ValueNode 
value_node, const T& expected_result) {
+  T conversion_target;
+  bool canConvert = value_node.getValue()->convertValue(conversion_target);
+  return canConvert && (expected_result == conversion_target);
+}
+
+TEST_CASE("IntValueNodeTests", "[responsenodevaluetests]") {
+  org::apache::nifi::minifi::state::response::ValueNode value_node;

Review comment:
       changed it in https://github.com/apache/nifi-minifi-cpp/pull/1065




-- 
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.

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


Reply via email to