]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_data_structures/profiling.rs
Auto merge of #68248 - JohnTitor:rollup-x0kml5f, r=JohnTitor
[rust.git] / src / librustc_data_structures / profiling.rs
index 066a6727bb49daf21e2930ed7a7979ef061e40c4..004db0a79a8808fbfc6e2681a828752cdd51acf9 100644 (file)
@@ -224,7 +224,10 @@ fn cold_call<F>(profiler_ref: &SelfProfilerRef, f: F) -> TimingGuard<'_>
     /// a measureme event, "verbose" generic activities also print a timing entry to
     /// stdout if the compiler is invoked with -Ztime or -Ztime-passes.
     #[inline(always)]
-    pub fn verbose_generic_activity<'a>(&'a self, event_id: &'a str) -> VerboseTimingGuard<'a> {
+    pub fn verbose_generic_activity<'a>(
+        &'a self,
+        event_id: &'static str,
+    ) -> VerboseTimingGuard<'a> {
         VerboseTimingGuard::start(
             event_id,
             self.print_verbose_generic_activities,
@@ -257,11 +260,7 @@ pub fn generic_activity(&self, event_id: &'static str) -> TimingGuard<'_> {
         self.exec(EventFilter::GENERIC_ACTIVITIES, |profiler| {
             let event_id = profiler.get_or_alloc_cached_string(event_id);
             let event_id = EventId::from_label(event_id);
-            TimingGuard::start(
-                profiler,
-                profiler.generic_activity_event_kind,
-                event_id
-            )
+            TimingGuard::start(profiler, profiler.generic_activity_event_kind, event_id)
         })
     }
 
@@ -290,11 +289,7 @@ pub fn query_cache_hit(&self, query_invocation_id: QueryInvocationId) {
     #[inline(always)]
     pub fn query_blocked(&self) -> TimingGuard<'_> {
         self.exec(EventFilter::QUERY_BLOCKED, |profiler| {
-            TimingGuard::start(
-                profiler,
-                profiler.query_blocked_event_kind,
-                EventId::INVALID,
-            )
+            TimingGuard::start(profiler, profiler.query_blocked_event_kind, EventId::INVALID)
         })
     }
 
@@ -339,6 +334,7 @@ pub fn with_profiler(&self, f: impl FnOnce(&SelfProfiler)) {
         }
     }
 
+    #[inline]
     pub fn enabled(&self) -> bool {
         self.profiler.is_some()
     }
@@ -437,7 +433,7 @@ pub fn get_or_alloc_cached_string(&self, s: &'static str) -> StringId {
             let string_cache = self.string_cache.read();
 
             if let Some(&id) = string_cache.get(s) {
-                return id
+                return id;
             }
         }
 
@@ -447,21 +443,14 @@ pub fn get_or_alloc_cached_string(&self, s: &'static str) -> StringId {
         *string_cache.entry(s).or_insert_with(|| self.profiler.alloc_string(s))
     }
 
-    pub fn map_query_invocation_id_to_string(
-        &self,
-        from: QueryInvocationId,
-        to: StringId
-    ) {
+    pub fn map_query_invocation_id_to_string(&self, from: QueryInvocationId, to: StringId) {
         let from = StringId::new_virtual(from.0);
         self.profiler.map_virtual_to_concrete_string(from, to);
     }
 
-    pub fn bulk_map_query_invocation_id_to_single_string<I>(
-        &self,
-        from: I,
-        to: StringId
-    )
-        where I: Iterator<Item=QueryInvocationId> + ExactSizeIterator
+    pub fn bulk_map_query_invocation_id_to_single_string<I>(&self, from: I, to: StringId)
+    where
+        I: Iterator<Item = QueryInvocationId> + ExactSizeIterator,
     {
         let from = from.map(|qid| StringId::new_virtual(qid.0));
         self.profiler.bulk_map_virtual_to_single_concrete_string(from, to);
@@ -506,6 +495,12 @@ pub fn finish_with_query_invocation_id(self, query_invocation_id: QueryInvocatio
     pub fn none() -> TimingGuard<'a> {
         TimingGuard(None)
     }
+
+    #[inline(always)]
+    pub fn run<R>(self, f: impl FnOnce() -> R) -> R {
+        let _timer = self;
+        f()
+    }
 }
 
 #[must_use]
@@ -574,39 +569,19 @@ fn get_resident() -> Option<usize> {
 
 #[cfg(windows)]
 fn get_resident() -> Option<usize> {
-    type BOOL = i32;
-    type DWORD = u32;
-    type HANDLE = *mut u8;
-    use libc::size_t;
-    #[repr(C)]
-    #[allow(non_snake_case)]
-    struct PROCESS_MEMORY_COUNTERS {
-        cb: DWORD,
-        PageFaultCount: DWORD,
-        PeakWorkingSetSize: size_t,
-        WorkingSetSize: size_t,
-        QuotaPeakPagedPoolUsage: size_t,
-        QuotaPagedPoolUsage: size_t,
-        QuotaPeakNonPagedPoolUsage: size_t,
-        QuotaNonPagedPoolUsage: size_t,
-        PagefileUsage: size_t,
-        PeakPagefileUsage: size_t,
-    }
-    #[allow(non_camel_case_types)]
-    type PPROCESS_MEMORY_COUNTERS = *mut PROCESS_MEMORY_COUNTERS;
-    #[link(name = "psapi")]
-    extern "system" {
-        fn GetCurrentProcess() -> HANDLE;
-        fn GetProcessMemoryInfo(
-            Process: HANDLE,
-            ppsmemCounters: PPROCESS_MEMORY_COUNTERS,
-            cb: DWORD,
-        ) -> BOOL;
-    }
-    let mut pmc: PROCESS_MEMORY_COUNTERS = unsafe { mem::zeroed() };
-    pmc.cb = mem::size_of_val(&pmc) as DWORD;
-    match unsafe { GetProcessMemoryInfo(GetCurrentProcess(), &mut pmc, pmc.cb) } {
+    use std::mem::{self, MaybeUninit};
+    use winapi::shared::minwindef::DWORD;
+    use winapi::um::processthreadsapi::GetCurrentProcess;
+    use winapi::um::psapi::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
+
+    let mut pmc = MaybeUninit::<PROCESS_MEMORY_COUNTERS>::uninit();
+    match unsafe {
+        GetProcessMemoryInfo(GetCurrentProcess(), pmc.as_mut_ptr(), mem::size_of_val(&pmc) as DWORD)
+    } {
         0 => None,
-        _ => Some(pmc.WorkingSetSize as usize),
+        _ => {
+            let pmc = unsafe { pmc.assume_init() };
+            Some(pmc.WorkingSetSize as usize)
+        }
     }
 }