Skip to content

Commit

Permalink
Add LookupAccountSidW caching #71
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Aug 11, 2020
1 parent 649bf40 commit b864530
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased](https://github.com/dalance/procs/compare/v0.10.4...Unreleased) - ReleaseDate

* [Added] LookupAccountSidW caching [#71](https://github.com/dalance/procs/issues/71)

## [v0.10.4](https://github.com/dalance/procs/compare/v0.10.3...v0.10.4) - 2020-08-10

* [Added] 256 colors support [#67](https://github.com/dalance/procs/issues/67)
Expand Down
29 changes: 28 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod watcher;
use crate::column::Column;
use crate::columns::*;
use crate::config::*;
use crate::util::adjust;
use crate::util::{adjust, lap};
use crate::view::View;
use crate::watcher::Watcher;
use anyhow::{Context, Error};
Expand All @@ -20,6 +20,7 @@ use std::cmp;
use std::collections::HashMap;
use std::fs;
use std::io::Read;
use std::time::Instant;
use structopt::{clap, StructOpt};
use unicode_width::UnicodeWidthStr;

Expand Down Expand Up @@ -140,6 +141,10 @@ pub struct Opt {
/// Generate configuration sample file
#[structopt(long = "config")]
pub config: bool,

/// Show debug message
#[structopt(long = "debug", hidden = true)]
pub debug: bool,
}

// ---------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -277,10 +282,32 @@ fn run_watch(opt: &Opt, config: &Config, interval: u64) -> Result<(), Error> {
}

fn run_default(opt: &Opt, config: &Config) -> Result<(), Error> {
let mut time = Instant::now();

let mut view = View::new(opt, config, false);

if opt.debug {
lap(&mut time, "Info: View::new");
}

view.filter(opt, config);

if opt.debug {
lap(&mut time, "Info: view.filter");
}

view.adjust(config, &HashMap::new());

if opt.debug {
lap(&mut time, "Info: view.adjust");
}

view.display(opt, config)?;

if opt.debug {
lap(&mut time, "Info: view.display");
}

Ok(())
}

Expand Down
24 changes: 22 additions & 2 deletions src/process/windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use chrono::offset::TimeZone;
use chrono::{Local, NaiveDate};
use libc::c_void;
use std::cell::RefCell;
use std::collections::HashMap;
use std::mem::{size_of, zeroed};
use std::ptr;
Expand Down Expand Up @@ -454,7 +455,7 @@ fn get_user(handle: HANDLE) -> Option<SidName> {
let psid = (*token_user).User.Sid;

let sid = get_sid(psid);
let (name, domainname) = if let Some((x, y)) = get_name(psid) {
let (name, domainname) = if let Some((x, y)) = get_name_cached(psid) {
(Some(x), Some(y))
} else {
(None, None)
Expand Down Expand Up @@ -510,7 +511,7 @@ fn get_groups(handle: HANDLE) -> Option<Vec<SidName>> {
for i in 0..(*token_groups).GroupCount {
let psid = (*sa.offset(i as isize)).Sid;
let sid = get_sid(psid);
let (name, domainname) = if let Some((x, y)) = get_name(psid) {
let (name, domainname) = if let Some((x, y)) = get_name_cached(psid) {
(Some(x), Some(y))
} else {
(None, None)
Expand Down Expand Up @@ -554,6 +555,25 @@ fn get_sid(psid: PSID) -> Vec<u64> {
}
}

thread_local!(
pub static NAME_CACHE: RefCell<HashMap<PSID, Option<(String, String)>>> =
{ RefCell::new(HashMap::new()) };
);

#[cfg_attr(tarpaulin, skip)]
fn get_name_cached(psid: PSID) -> Option<(String, String)> {
NAME_CACHE.with(|c| {
let mut c = c.borrow_mut();
if let Some(x) = c.get(&psid) {
x.clone()
} else {
let x = get_name(psid);
c.insert(psid, x.clone());
x
}
})
}

#[cfg_attr(tarpaulin, skip)]
fn get_name(psid: PSID) -> Option<(String, String)> {
unsafe {
Expand Down
12 changes: 12 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::columns::{ConfigColumnKind, KIND_LIST};
use crate::config::{ConfigColumnAlign, ConfigSearchLogic};
use byte_unit::Byte;
use std::borrow::Cow;
use std::time::Instant;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

pub enum KeywordClass {
Expand Down Expand Up @@ -222,3 +223,14 @@ pub fn bytify(x: u64) -> String {
.replace("B", "")
.replace("i", "")
}

pub fn lap(instant: &mut Instant, msg: &str) {
let period = instant.elapsed();
eprintln!(
"{} [{}.{:03}s]",
msg,
period.as_secs(),
period.subsec_nanos() / 1000000
);
instant.clone_from(&Instant::now());
}

0 comments on commit b864530

Please sign in to comment.