diff --git a/dbms/src/Common/ProcessCollector.cpp b/dbms/src/Common/ProcessCollector.cpp index 6615803e02e..eadb0c39efe 100644 --- a/dbms/src/Common/ProcessCollector.cpp +++ b/dbms/src/Common/ProcessCollector.cpp @@ -18,44 +18,71 @@ namespace DB { -ProcessCollector::ProcessCollector() -{ - auto info = get_process_metrics(); - start_time.Set(info.start_time); -} - std::vector ProcessCollector::Collect() const { auto new_info = get_process_metrics(); - // Gauge is thread safe, no need to lock. - auto past_cpu_total = cpu_total.Value(); - cpu_total.Increment(new_info.cpu_total - past_cpu_total); - vsize.Set(new_info.vsize); - rss.Set(new_info.rss); - std::vector familes; - familes.reserve(4); - familes.emplace_back(prometheus::MetricFamily{ - CPU_METRIC_NAME, - CPU_METRIC_HELP, - prometheus::MetricType::Gauge, - std::vector{cpu_total.Collect()}}); - familes.emplace_back(prometheus::MetricFamily{ - VSIZE_METRIC_NAME, - VSIZE_METRIC_HELP, - prometheus::MetricType::Gauge, - std::vector{vsize.Collect()}}); - familes.emplace_back(prometheus::MetricFamily{ - RSS_METRIC_NAME, - RSS_METRIC_HELP, - prometheus::MetricType::Gauge, - std::vector{rss.Collect()}}); + + // The following metrics shadow TiFlash proxy metrics, so that we ensure these metrics are available + // in disaggregated mode, where TiFlash proxy may not start at all. + // Note that, even in non-disaggregated mode, duplicates are fine when being collected by Prometheus, + // because TiFlash proxy and TiFlash have different metrics endpoints. However we will see multiple + // endpoints in the Grafana, because both TiFlash proxy and TiFlash uses the same metric name. + // To avoid duplicates in Grafana, we will only include proxy metrics when proxy is not enabled. + if (include_proxy_metrics) + { + familes.emplace_back(prometheus::MetricFamily{ + "tiflash_proxy_process_cpu_seconds_total", + "Total user and system CPU time spent in seconds.", + prometheus::MetricType::Gauge, + { + prometheus::ClientMetric{.gauge = {static_cast(new_info.cpu_total)}}, + }}); + + familes.emplace_back(prometheus::MetricFamily{ + "tiflash_proxy_process_virtual_memory_bytes", + "Virtual memory size in bytes.", + prometheus::MetricType::Gauge, + { + prometheus::ClientMetric{.gauge = {static_cast(new_info.vsize)}}, + }}); + familes.emplace_back(prometheus::MetricFamily{ + "tiflash_proxy_process_resident_memory_bytes", + "Resident memory size in bytes.", + prometheus::MetricType::Gauge, + { + prometheus::ClientMetric{.gauge = {static_cast(new_info.rss)}}, + }}); + familes.emplace_back(prometheus::MetricFamily{ + "tiflash_proxy_process_start_time_seconds", + "Start time of the process since unix epoch in seconds.", + prometheus::MetricType::Gauge, + { + prometheus::ClientMetric{.gauge = {static_cast(new_info.start_time)}}, + }}); + } + + // The following metrics are TiFlash specific process metrics. familes.emplace_back(prometheus::MetricFamily{ - START_TIME_METRIC_NAME, - START_TIME_METRIC_HELP, + "tiflash_process_rss_by_type_bytes", + "Resident memory size by type in bytes.", prometheus::MetricType::Gauge, - std::vector{start_time.Collect()}}); + { + prometheus::ClientMetric{ + .label = {{"type", "anon"}}, + .gauge = {static_cast(new_info.rss_anon)}, + }, + prometheus::ClientMetric{ + .label = {{"type", "file"}}, + .gauge = {static_cast(new_info.rss_file)}, + }, + prometheus::ClientMetric{ + .label = {{"type", "shared"}}, + .gauge = {static_cast(new_info.rss_shared)}, + }, + }}); + return familes; } diff --git a/dbms/src/Common/ProcessCollector.h b/dbms/src/Common/ProcessCollector.h index 51f4723be70..4ad9636b919 100644 --- a/dbms/src/Common/ProcessCollector.h +++ b/dbms/src/Common/ProcessCollector.h @@ -14,8 +14,10 @@ #pragma once +#include #include #include +#include #include #include @@ -27,28 +29,13 @@ namespace DB // Just like the original tiflash-proxy logic. // 2. Current implentation of async_metrics interval is 15s, it's too large. And this interval also affect pushgateway interval. // So better not to mix cpu/mem metrics with async_metrics. -// The difference between ProcessCollector and prometheus::Registry: -// 1. ProcessCollector will **update** Gauge then collect. prometheus::Registry only collect Gauge. class ProcessCollector : public prometheus::Collectable { public: - static constexpr auto CPU_METRIC_NAME = "tiflash_proxy_process_cpu_seconds_total"; - static constexpr auto CPU_METRIC_HELP = "Total user and system CPU time spent in seconds."; - static constexpr auto VSIZE_METRIC_NAME = "tiflash_proxy_process_virtual_memory_bytes"; - static constexpr auto VSIZE_METRIC_HELP = "Virtual memory size in bytes."; - static constexpr auto RSS_METRIC_NAME = "tiflash_proxy_process_resident_memory_bytes"; - static constexpr auto RSS_METRIC_HELP = "Resident memory size in bytes."; - static constexpr auto START_TIME_METRIC_NAME = "tiflash_proxy_process_start_time_seconds"; - static constexpr auto START_TIME_METRIC_HELP = "Start time of the process since unix epoch in seconds."; - - ProcessCollector(); - std::vector Collect() const override; -private: - mutable prometheus::Gauge cpu_total; - mutable prometheus::Gauge vsize; - mutable prometheus::Gauge rss; - prometheus::Gauge start_time; +public: + mutable std::atomic include_proxy_metrics = {true}; }; + } // namespace DB diff --git a/dbms/src/Common/ProcessCollector_fwd.h b/dbms/src/Common/ProcessCollector_fwd.h new file mode 100644 index 00000000000..b523490fd7c --- /dev/null +++ b/dbms/src/Common/ProcessCollector_fwd.h @@ -0,0 +1,22 @@ +// Copyright 2023 PingCAP, Inc. +// +// Licensed 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 + +namespace DB +{ + +class ProcessCollector; + +} diff --git a/dbms/src/Common/TiFlashMetrics.cpp b/dbms/src/Common/TiFlashMetrics.cpp index f7f47bac33a..86315a21c1d 100644 --- a/dbms/src/Common/TiFlashMetrics.cpp +++ b/dbms/src/Common/TiFlashMetrics.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include #include @@ -27,6 +28,8 @@ TiFlashMetrics & TiFlashMetrics::instance() TiFlashMetrics::TiFlashMetrics() { + process_collector = std::make_shared(); + registered_profile_events.reserve(ProfileEvents::end()); for (ProfileEvents::Event event = 0; event < ProfileEvents::end(); event++) { @@ -202,4 +205,9 @@ void TiFlashMetrics::registerStorageThreadMemory(const std::string & k) } } -} // namespace DB \ No newline at end of file +void TiFlashMetrics::setProvideProxyProcessMetrics(bool v) +{ + process_collector->include_proxy_metrics = v; +} + +} // namespace DB diff --git a/dbms/src/Common/TiFlashMetrics.h b/dbms/src/Common/TiFlashMetrics.h index 1703ec4387a..0b3f99aa42c 100644 --- a/dbms/src/Common/TiFlashMetrics.h +++ b/dbms/src/Common/TiFlashMetrics.h @@ -16,7 +16,7 @@ #include #include -#include +#include #include #include #include @@ -1089,6 +1089,7 @@ struct MetricFamily return *(resource_group_metrics_map[resource_group_name][idx]); } + private: void addMetricsForResourceGroup(const String & resource_group_name) { @@ -1143,6 +1144,7 @@ class TiFlashMetrics double getStorageThreadMemory(MemoryAllocType type, const std::string & k); void registerProxyThreadMemory(const std::string & k); void registerStorageThreadMemory(const std::string & k); + void setProvideProxyProcessMetrics(bool v); private: TiFlashMetrics(); @@ -1157,10 +1159,7 @@ class TiFlashMetrics static constexpr auto storages_thread_memory_usage = "tiflash_storages_thread_memory_usage"; std::shared_ptr registry = std::make_shared(); - // Here we add a ProcessCollector to collect cpu/rss/vsize/start_time information. - // Normally, these metrics will be collected by tiflash-proxy, - // but in disaggregated compute mode with AutoScaler, tiflash-proxy will not start, so tiflash will collect these metrics itself. - std::shared_ptr cn_process_collector = std::make_shared(); + std::shared_ptr process_collector; std::vector registered_profile_events; std::vector registered_current_metrics; diff --git a/dbms/src/Server/MetricsPrometheus.cpp b/dbms/src/Server/MetricsPrometheus.cpp index 7d99611b195..f030f7902de 100644 --- a/dbms/src/Server/MetricsPrometheus.cpp +++ b/dbms/src/Server/MetricsPrometheus.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -207,6 +208,11 @@ MetricsPrometheus::MetricsPrometheus(Context & context, const AsynchronousMetric auto & tiflash_metrics = TiFlashMetrics::instance(); auto & conf = context.getConfigRef(); + bool should_provide_proxy_metrics + = (context.getSharedContextDisagg()->isDisaggregatedComputeMode() + && context.getSharedContextDisagg()->use_autoscaler); + tiflash_metrics.setProvideProxyProcessMetrics(should_provide_proxy_metrics); + // Interval to collect `ProfileEvents::Event`/`CurrentMetrics::Metric`/`AsynchronousMetrics` // When push mode is enabled, it also define the interval that Prometheus client push to pushgateway. metrics_interval = conf.getInt(status_metrics_interval, 15); @@ -245,11 +251,7 @@ MetricsPrometheus::MetricsPrometheus(Context & context, const AsynchronousMetric const auto & labels = prometheus::Gateway::GetInstanceLabel(getInstanceValue(conf)); gateway = std::make_shared(host, port, job_name, labels); gateway->RegisterCollectable(tiflash_metrics.registry); - if (context.getSharedContextDisagg()->isDisaggregatedComputeMode() - && context.getSharedContextDisagg()->use_autoscaler) - { - gateway->RegisterCollectable(tiflash_metrics.cn_process_collector); - } + gateway->RegisterCollectable(tiflash_metrics.process_collector); LOG_INFO(log, "Enable prometheus push mode; interval = {}; addr = {}", metrics_interval, metrics_addr); } @@ -268,12 +270,9 @@ MetricsPrometheus::MetricsPrometheus(Context & context, const AsynchronousMetric addr = listen_host + ":" + metrics_port; if (context.getSecurityConfig()->hasTlsConfig() && !conf.getBool(status_disable_metrics_tls, false)) { - std::vector> collectables{tiflash_metrics.registry}; - if (context.getSharedContextDisagg()->isDisaggregatedComputeMode() - && context.getSharedContextDisagg()->use_autoscaler) - { - collectables.push_back(tiflash_metrics.cn_process_collector); - } + std::vector> collectables{ + tiflash_metrics.registry, + tiflash_metrics.process_collector}; server = getHTTPServer(context, collectables, addr); server->start(); LOG_INFO( @@ -286,11 +285,7 @@ MetricsPrometheus::MetricsPrometheus(Context & context, const AsynchronousMetric { exposer = std::make_shared(addr); exposer->RegisterCollectable(tiflash_metrics.registry); - if (context.getSharedContextDisagg()->isDisaggregatedComputeMode() - && context.getSharedContextDisagg()->use_autoscaler) - { - exposer->RegisterCollectable(tiflash_metrics.cn_process_collector); - } + exposer->RegisterCollectable(tiflash_metrics.process_collector); LOG_INFO( log, "Enable prometheus pull mode; Listen Host = {}, Metrics Port = {}", diff --git a/libs/libprocess_metrics/.gitignore b/libs/libprocess_metrics/.gitignore new file mode 100644 index 00000000000..54466f5b094 --- /dev/null +++ b/libs/libprocess_metrics/.gitignore @@ -0,0 +1,2 @@ +/target + diff --git a/libs/libprocess_metrics/CMakeLists.txt b/libs/libprocess_metrics/CMakeLists.txt index 3d4cb1f90ad..2cfdd32f968 100644 --- a/libs/libprocess_metrics/CMakeLists.txt +++ b/libs/libprocess_metrics/CMakeLists.txt @@ -20,6 +20,7 @@ add_custom_command(OUTPUT ${_PROCESS_METRICS_LIBRARY} COMMENT "Building process_metrics" COMMAND cargo build --release --target-dir ${CMAKE_CURRENT_BINARY_DIR} VERBATIM + USES_TERMINAL WORKING_DIRECTORY ${_PROCESS_METRICS_SOURCE_DIR} DEPENDS "${_PROCESS_METRICS_SRCS}" "${_PROCESS_METRICS_HEADERS}" diff --git a/libs/libprocess_metrics/Cargo.lock b/libs/libprocess_metrics/Cargo.lock index e389bac0c3d..12a11032728 100644 --- a/libs/libprocess_metrics/Cargo.lock +++ b/libs/libprocess_metrics/Cargo.lock @@ -4,15 +4,19 @@ version = 3 [[package]] name = "bitflags" -version = "1.3.2" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" [[package]] -name = "byteorder" -version = "1.4.3" +name = "errno" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys", +] [[package]] name = "hex" @@ -28,9 +32,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.141" +version = "0.2.154" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" +checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" [[package]] name = "libprocess_metrics" @@ -38,93 +42,120 @@ version = "0.1.0" dependencies = [ "lazy_static", "libc", - "page_size", "procfs", - "procinfo", ] [[package]] -name = "nom" -version = "2.2.1" +name = "linux-raw-sys" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf51a729ecf40266a2368ad335a5fdde43471f545a967109cd62146ecf8b66ff" +checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" [[package]] -name = "page_size" -version = "0.4.2" +name = "procfs" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eebde548fbbf1ea81a99b128872779c437752fb99f217c45245e1a61dcd9edcd" +checksum = "731e0d9356b0c25f16f33b5be79b1c57b562f141ebfcdb0ad8ac2c13a24293b4" dependencies = [ - "libc", - "winapi", + "bitflags", + "hex", + "lazy_static", + "procfs-core", + "rustix", ] [[package]] -name = "procfs" -version = "0.12.0" +name = "procfs-core" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0941606b9934e2d98a3677759a971756eb821f75764d0e0d26946d08e74d9104" +checksum = "2d3554923a69f4ce04c4a754260c338f505ce22642d3830e049a399fc2059a29" dependencies = [ "bitflags", - "byteorder", "hex", - "lazy_static", - "libc", ] [[package]] -name = "procinfo" -version = "0.4.2" -source = "git+https://github.com/tikv/procinfo-rs?rev=6599eb9dca74229b2c1fcc44118bef7eff127128#6599eb9dca74229b2c1fcc44118bef7eff127128" +name = "rustix" +version = "0.38.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ - "byteorder", + "bitflags", + "errno", "libc", - "nom", - "rustc_version", + "linux-raw-sys", + "windows-sys", ] [[package]] -name = "rustc_version" -version = "0.2.3" +name = "windows-sys" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "semver", + "windows-targets", ] [[package]] -name = "semver" -version = "0.9.0" +name = "windows-targets" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" dependencies = [ - "semver-parser", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] [[package]] -name = "semver-parser" -version = "0.7.0" +name = "windows_aarch64_gnullvm" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" [[package]] -name = "winapi" -version = "0.3.9" +name = "windows_aarch64_msvc" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" [[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" +name = "windows_x86_64_gnullvm" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" [[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" +name = "windows_x86_64_msvc" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" diff --git a/libs/libprocess_metrics/Cargo.toml b/libs/libprocess_metrics/Cargo.toml index 89206dc7779..3d492ea7f90 100644 --- a/libs/libprocess_metrics/Cargo.toml +++ b/libs/libprocess_metrics/Cargo.toml @@ -3,8 +3,6 @@ name = "libprocess_metrics" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [lib] name = "process_metrics" crate-type = ["staticlib"] @@ -12,7 +10,6 @@ crate-type = ["staticlib"] [dependencies] lazy_static = "1.3" libc = "0.2" + [target.'cfg(target_os = "linux")'.dependencies] -procinfo = { git = "https://github.com/tikv/procinfo-rs", rev = "6599eb9dca74229b2c1fcc44118bef7eff127128" } -page_size = "0.4" -procfs = { version = "0.12", default-features = false } +procfs = { version = "0.16", default-features = false } diff --git a/libs/libprocess_metrics/include/ProcessMetrics/ProcessMetrics.h b/libs/libprocess_metrics/include/ProcessMetrics/ProcessMetrics.h index d7b68d276e8..8708db9707b 100644 --- a/libs/libprocess_metrics/include/ProcessMetrics/ProcessMetrics.h +++ b/libs/libprocess_metrics/include/ProcessMetrics/ProcessMetrics.h @@ -23,8 +23,11 @@ extern "C" { struct ProcessMetricsInfo { uint64_t cpu_total; - int64_t vsize; - int64_t rss; + uint64_t vsize; + uint64_t rss; + uint64_t rss_anon; + uint64_t rss_file; + uint64_t rss_shared; int64_t start_time; }; diff --git a/libs/libprocess_metrics/src/lib.rs b/libs/libprocess_metrics/src/lib.rs index 1c8aeb73e4f..c193c99148c 100644 --- a/libs/libprocess_metrics/src/lib.rs +++ b/libs/libprocess_metrics/src/lib.rs @@ -13,24 +13,17 @@ // limitations under the License. #[repr(C)] +#[derive(Default)] pub struct ProcessMetricsInfo { pub cpu_total: u64, - pub vsize: i64, - pub rss: i64, + pub vsize: u64, + pub rss: u64, + pub rss_anon: u64, + pub rss_file: u64, + pub rss_shared: u64, pub start_time: i64, } -impl Default for ProcessMetricsInfo { - fn default() -> Self { - ProcessMetricsInfo { - cpu_total: 0, - vsize: 0, - rss: 0, - start_time: 0, - } - } -} - #[cfg(target_os = "linux")] #[inline] pub fn ticks_per_second() -> i64 { @@ -57,35 +50,31 @@ pub extern "C" fn get_process_metrics() -> ProcessMetricsInfo { } #[cfg(target_os = "linux")] -#[no_mangle] -pub extern "C" fn get_process_metrics() -> ProcessMetricsInfo { - let p = match procfs::process::Process::myself() { - Ok(p) => p, - Err(..) => { - // we can't construct a Process object, so there's no stats to gather - return ProcessMetricsInfo::default(); - } - }; - - let mut start_time: i64 = 0; +fn get_proces_metrics_linux() -> procfs::ProcResult { + let p = procfs::process::Process::myself()?; + let stat = p.stat()?; + let status = p.status()?; + + let mut start_time = 0i64; if let Ok(boot_time) = procfs::boot_time_secs() { - start_time = p.stat.starttime as i64 / ticks_per_second() + boot_time as i64; + start_time = stat.starttime as i64 / ticks_per_second() + boot_time as i64; } - ProcessMetricsInfo { - cpu_total: (p.stat.utime + p.stat.stime) / ticks_per_second() as u64, - vsize: p.stat.vsize as i64, - rss: p.stat.rss * *PAGESIZE, + + Ok(ProcessMetricsInfo { + cpu_total: (stat.utime + stat.stime) / ticks_per_second() as u64, + vsize: status.vmsize.unwrap_or_default() * 1024, + rss: status.vmrss.unwrap_or_default() * 1024, + rss_anon: status.rssanon.unwrap_or_default() * 1024, + rss_file: status.rssfile.unwrap_or_default() * 1024, + rss_shared: status.rssshmem.unwrap_or_default() * 1024, start_time, - } + }) } -lazy_static::lazy_static! { - // getconf PAGESIZE - static ref PAGESIZE: i64 = { - unsafe { - libc::sysconf(libc::_SC_PAGESIZE) - } - }; +#[cfg(target_os = "linux")] +#[no_mangle] +pub extern "C" fn get_process_metrics() -> ProcessMetricsInfo { + get_proces_metrics_linux().unwrap_or_default() } lazy_static::lazy_static! {