Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[virtual tables] Build cpu_time table for Windows #8040

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 80 additions & 0 deletions osquery/tables/system/windows/cpu_time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright (c) 2014-present, The osquery authors
*
* This source code is licensed as defined by the LICENSE file found in the
* root directory of this source tree.
*
* SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)
*/

#include "osquery/core/windows/wmi.h"
#include <osquery/core/system.h>
#include <osquery/core/tables.h>
#include <osquery/logger/logger.h>
#include <osquery/sql/sql.h>
#include <osquery/utils/conversions/tryto.h>

#include <unordered_map>

namespace osquery {
namespace tables {

QueryData genCpuTime(QueryContext& context) {
QueryData results;

const Expected<WmiRequest, WmiError> wmiSystemReq_uptime =
WmiRequest::CreateWmiRequest(
"select ElapsedTime, Name from "
"Win32_PerfFormattedData_PerfProc_Process");
if (!wmiSystemReq_uptime || wmiSystemReq_uptime->results().empty()) {
LOG(WARNING) << "Error retrieving information from WMI.";
return results;
}

const std::vector<WmiResultItem>& uptimeData = wmiSystemReq_uptime->results();
std::unordered_map<std::string, long long> uptimeMap;
for (const auto& data : uptimeData) {
std::string name;
data.GetString("Name", name);
long long temp = 0;
data.GetLongLong("ElapsedTime", temp);
uptimeMap[name] = temp; // in seconds
}

const Expected<WmiRequest, WmiError> wmiSystemReq =
WmiRequest::CreateWmiRequest(
"select Name, PercentUserTime, PercentPrivilegedTime, "
"PercentIdleTime, PercentInterruptTime, PercentPriorityTime "
"from Win32_PerfFormattedData_Counters_ProcessorInformation");
if (!wmiSystemReq || wmiSystemReq->results().empty()) {
LOG(WARNING) << "Error retrieving information from WMI.";
return results;
}

const std::vector<WmiResultItem>& wmiResults = wmiSystemReq->results();
for (const auto& data : wmiResults) {
std::string name;
data.GetString("Name", name);
r["core"] = name;
long long uptime = uptimeMap[name];
long percent = 0;
data.GetLongLong("PercentUserTime", percent);
// Hundredths of a second, percent / 100 * uptime * 100
r["user"] = BIGINT(percent * uptime);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm. I think this is going to be weird. We already see some issues around uptime not behaving as expected, and I think the lack of precision in percent is going to make this jump around.

Copy link
Contributor Author

@yux-m yux-m May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help! I've moved the spec file.

I'm still looking for a better source of uptime or specific times in windows, and yeah, I think the precision is a real problem - elapsed time given by wmi is in seconds as int, and the percentages are also integers. (It probably didn't make sense to use 0.01 sec as the unit.)

In case it doesn't work out, do you think it's a good idea to provide the percents instead, maybe in a new table just for windows?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you move or add a spec file (any file that contributes in the build really), you also have to modify the respective CMakeLists.txt.
In this case https://github.com/osquery/osquery/blob/master/specs/CMakeLists.txt; you would have to move the file from the platform_dependent to the independent one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the guidance!

data.GetLongLong("PercentPrivilegedTime", percent);
r["system"] = BIGINT(percent * uptime);
data.GetLongLong("PercentIdleTime", percent);
r["idle"] = BIGINT(percent * uptime);
long idle = percent;
data.GetLongLong("PercentInterruptTime", percent);
r["irq"] = BIGINT(percent * uptime);
// Time spent on low priority threads
data.GetLongLong("PercentPriorityTime", percent);
r["nice"] = BIGINT((100 - percent - idle) * uptime);
results.push_back(r);
}

return results;
}
} // namespace tables
} // namespace osquery
2 changes: 1 addition & 1 deletion specs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function(generateNativeTables)
certificates.table
chrome_extensions.table
chrome_extension_content_scripts.table
cpu_time.table
curl.table
curl_certificate.table
etc_hosts.table
Expand Down Expand Up @@ -199,7 +200,6 @@ function(generateNativeTables)
"posix/augeas.table:linux,macos"
"posix/authorized_keys.table:linux,macos"
"posix/block_devices.table:linux,macos"
"posix/cpu_time.table:linux,macos"
"posix/crontab.table:linux,macos"
"posix/disk_encryption.table:linux,macos"
"posix/dns_resolvers.table:linux,macos"
Expand Down
File renamed without changes.