From 5fc220fb1d39da11c9d41fdd6611e4455cc22eba Mon Sep 17 00:00:00 2001 From: "Niall Douglas (s [underscore] sourceforge {at} nedprod [dot] com)" Date: Fri, 20 Aug 2021 16:49:48 +0100 Subject: Fix utils::current_process_memory() on Linux which could enter an infinite loop. --- include/llfio/v2.0/detail/impl/posix/utils.ipp | 47 +++++++++++++------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/include/llfio/v2.0/detail/impl/posix/utils.ipp b/include/llfio/v2.0/detail/impl/posix/utils.ipp index d0b21ba2..7b734a45 100644 --- a/include/llfio/v2.0/detail/impl/posix/utils.ipp +++ b/include/llfio/v2.0/detail/impl/posix/utils.ipp @@ -28,6 +28,8 @@ Distributed under the Boost Software License, Version 1.0. #include +// #include + #ifdef __linux__ #include // for preadv #endif @@ -267,35 +269,31 @@ namespace utils std::vector anon_entries, non_anon_entries; anon_entries.reserve(32); non_anon_entries.reserve(32); - auto sizeidx = totalview.find("\nSize:"); - while(sizeidx < totalview.size()) - { - auto itemtopidx = totalview.rfind("\n", sizeidx - 1); - if(string_view::npos == itemtopidx) - { - itemtopidx = 0; - } - // hexaddr-hexaddr flags offset dev:id inode [path] - size_t begin, end, offset, inode = 1; - char f1, f2, f3, f4, f5, f6, f7, f8; - sscanf(totalview.data() + itemtopidx, "%zx-%zx %c%c%c%c %zx %c%c:%c%c %zu", &begin, &end, &f1, &f2, &f3, &f4, &offset, &f5, &f6, &f7, &f8, &inode); - sizeidx = totalview.find("\nSize:", sizeidx + 1); - if(string_view::npos == sizeidx) + auto find_item = [&](size_t idx) -> string_view { + auto x = totalview.rfind("\nSize:", idx); + if(x == string_view::npos) { - sizeidx = totalview.size(); + return {}; } - auto itemendidx = totalview.rfind("\n", sizeidx - 1); - if(string_view::npos == itemendidx) + x = totalview.rfind("\n", x - 1); + if(x == string_view::npos) { - abort(); + x = 0; } - const string_view item(totalview.substr(itemtopidx + 1, itemendidx - itemtopidx - 1)); - auto vmflagsidx = item.rfind("\n"); - if(string_view::npos == itemendidx) + else { - abort(); + x++; } - if(0 != memcmp(item.data() + vmflagsidx, "\nVmFlags:", 9)) + return totalview.substr(x, idx - x); + }; + for(string_view item = find_item(totalview.size()); item != string_view(); item = find_item(item.data() - totalview.data())) + { + //std::cout << "***" << item << "***"; + // hexaddr-hexaddr flags offset dev:id inode [path] + size_t inode = 1; + sscanf(item.data(), "%*x-%*x %*c%*c%*c%*c %*x %*c%*c:%*c%*c %zu", &inode); + auto vmflagsidx = item.rfind("\nVmFlags:"); + if(vmflagsidx == string_view::npos) { return errc::illegal_byte_sequence; } @@ -559,7 +557,8 @@ namespace utils mach_timebase_info_data_t timebase; mach_timebase_info(&timebase); return (double) timebase.numer / timebase.denom; - }();*/ 10000000.0; // no idea why, but apparently this is the multiplier according to Mac CI runners + }();*/ + 10000000.0; // no idea why, but apparently this is the multiplier according to Mac CI runners ret.system_ns_in_user_mode = (uint64_t)(ts_multiplier * ret.system_ns_in_user_mode); ret.system_ns_in_kernel_mode = (uint64_t)(ts_multiplier * ret.system_ns_in_kernel_mode); ret.system_ns_in_idle_mode = (uint64_t)(ts_multiplier * ret.system_ns_in_idle_mode); -- cgit v1.2.3