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



##########
File path: extensions/procfs/CpuStat.h
##########
@@ -0,0 +1,140 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <sstream>
+#include <string>
+#include <utility>
+#include <optional>
+#include "rapidjson/document.h"
+
+namespace org::apache::nifi::minifi::procfs {
+
+class CpuStatData {
+  CpuStatData() = default;
+
+ public:
+  CpuStatData(const CpuStatData& src) = default;
+  CpuStatData(CpuStatData&& src) noexcept = default;
+
+  static std::optional<CpuStatData> parseCpuStatLine(std::istringstream& iss);
+
+  bool operator>=(const CpuStatData &rhs) const;
+  bool operator==(const CpuStatData &rhs) const;
+  CpuStatData operator-(const CpuStatData &rhs) const;
+
+  uint64_t getUser() const { return user_; }
+  uint64_t getNice() const { return nice_; }
+  uint64_t getSystem() const { return system_; }
+  uint64_t getIdle() const { return idle_; }
+  uint64_t getIoWait() const { return io_wait_; }
+  uint64_t getIrq() const { return irq_; }
+  uint64_t getSoftIrq() const { return soft_irq_; }
+  uint64_t getSteal() const { return steal_; }
+  uint64_t getGuest() const { return guest_; }
+  uint64_t getGuestNice() const { return guest_nice_; }
+
+  uint64_t getIdleAll() const { return idle_ + io_wait_; }
+  uint64_t getSystemAll() const { return system_ + irq_ + soft_irq_; }
+  uint64_t getVirtAll() const { return guest_ + guest_nice_; }
+  uint64_t getTotal() const { return user_ + nice_ + getSystemAll() + 
getIdleAll() + steal_ + getVirtAll(); }

Review comment:
       good idea, added them in 
https://github.com/apache/nifi-minifi-cpp/pull/1152/commits/f606e41ffaf49145aefa8ff3c21fe0108a42e270

##########
File path: extensions/procfs/CpuStat.cpp
##########
@@ -0,0 +1,116 @@
+/**
+ * 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 "CpuStat.h"
+
+namespace org::apache::nifi::minifi::procfs {
+
+std::optional<CpuStatData> CpuStatData::parseCpuStatLine(std::istringstream& 
iss) {
+  CpuStatData data;
+  iss >> data.user_ >> data.nice_ >> data.system_ >> data.idle_ >> 
data.io_wait_ >> data.irq_ >> data.soft_irq_ >> data.steal_ >> data.guest_ >> 
data.guest_nice_;
+  if (iss.fail())
+    return std::nullopt;
+  data.user_ -= data.guest_;  // Guest time is already accounted in usertime
+  data.nice_ -= data.guest_nice_;
+  return data;
+}
+
+bool CpuStatData::operator>=(const CpuStatData &rhs) const {
+  return user_ >= rhs.user_
+         && nice_ >= rhs.nice_
+         && system_ >= rhs.system_
+         && idle_ >= rhs.idle_
+         && io_wait_ >= rhs.io_wait_
+         && irq_ >= rhs.irq_
+         && soft_irq_ >= rhs.soft_irq_
+         && steal_ >= rhs.steal_
+         && guest_ >= rhs.guest_
+         && guest_nice_ >= rhs.guest_nice_;
+}

Review comment:
       makes sense :) updated the PR to use the spaceship where possible 
https://github.com/apache/nifi-minifi-cpp/pull/1152/commits/f606e41ffaf49145aefa8ff3c21fe0108a42e270




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


Reply via email to