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

Performance Overlay #4620

Merged
merged 6 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 243 additions & 0 deletions Utilities/CPUStats.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
#pragma once

#include <Utilities/types.h>

#ifdef _WIN32
#include "windows.h"
#include "tlhelp32.h"
#else
#include "stdlib.h"
#include "sys/times.h"
#include "sys/types.h"
#include "unistd.h"
#endif

#ifdef __APPLE__
# include <mach/mach_init.h>
# include <mach/task.h>
# include <mach/vm_map.h>
#endif

#ifdef __linux__
# include <dirent.h>
#endif

#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
# include <sys/sysctl.h>
# if defined(__DragonFly__) || defined(__FreeBSD__)
# include <sys/user.h>
# endif

# if defined(__NetBSD__)
# undef KERN_PROC
# define KERN_PROC KERN_PROC2
# define kinfo_proc kinfo_proc2
# endif

# if defined(__DragonFly__)
# define KP_NLWP(kp) (kp.kp_nthreads)
# elif defined(__FreeBSD__)
# define KP_NLWP(kp) (kp.ki_numthreads)
# elif defined(__NetBSD__)
# define KP_NLWP(kp) (kp.p_nlwps)
# endif
#endif

class CPUStats
{
#ifdef _WIN32
HANDLE m_self;
using time_type = ULARGE_INTEGER;
#else
using time_type = clock_t;
#endif

private:
s32 m_num_processors;
time_type m_last_cpu, m_sys_cpu, m_usr_cpu;

public:
CPUStats()
{
#ifdef _WIN32
SYSTEM_INFO sysInfo;
FILETIME ftime, fsys, fuser;

GetSystemInfo(&sysInfo);
m_num_processors = sysInfo.dwNumberOfProcessors;

GetSystemTimeAsFileTime(&ftime);
memcpy(&m_last_cpu, &ftime, sizeof(FILETIME));

m_self = GetCurrentProcess();
GetProcessTimes(m_self, &ftime, &ftime, &fsys, &fuser);
memcpy(&m_sys_cpu, &fsys, sizeof(FILETIME));
memcpy(&m_usr_cpu, &fuser, sizeof(FILETIME));
#else
struct tms timeSample;

m_last_cpu = times(&timeSample);
m_sys_cpu = timeSample.tms_stime;
m_usr_cpu = timeSample.tms_utime;
m_num_processors = sysconf(_SC_NPROCESSORS_ONLN);
#endif
}

double get_usage()
{
#ifdef _WIN32
FILETIME ftime, fsys, fusr;
ULARGE_INTEGER now, sys, usr;

GetSystemTimeAsFileTime(&ftime);
memcpy(&now, &ftime, sizeof(FILETIME));

GetProcessTimes(m_self, &ftime, &ftime, &fsys, &fusr);
memcpy(&sys, &fsys, sizeof(FILETIME));
memcpy(&usr, &fusr, sizeof(FILETIME));
double percent = (sys.QuadPart - m_sys_cpu.QuadPart) + (usr.QuadPart - m_usr_cpu.QuadPart);
percent /= (now.QuadPart - m_last_cpu.QuadPart);
percent /= m_num_processors;

m_last_cpu = now;
m_usr_cpu = usr;
m_sys_cpu = sys;

return std::clamp(percent * 100, 0.0, 100.0);
#else
struct tms timeSample;
clock_t now;
double percent;

now = times(&timeSample);
if (now <= m_last_cpu || timeSample.tms_stime < m_sys_cpu || timeSample.tms_utime < m_usr_cpu)
{
// Overflow detection. Just skip this value.
percent = -1.0;
}
else
{
percent = (timeSample.tms_stime - m_sys_cpu) + (timeSample.tms_utime - m_usr_cpu);
percent /= (now - m_last_cpu);
percent /= m_num_processors;
percent *= 100;
}
m_last_cpu = now;
m_sys_cpu = timeSample.tms_stime;
m_usr_cpu = timeSample.tms_utime;

return percent;
#endif
}

static u32 get_thread_count()
{
#ifdef _WIN32
// first determine the id of the current process
DWORD const id = GetCurrentProcessId();

// then get a process list snapshot.
HANDLE const snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);

// initialize the process entry structure.
PROCESSENTRY32 entry = {0};
entry.dwSize = sizeof(entry);

// get the first process info.
BOOL ret = true;
ret = Process32First(snapshot, &entry);
while (ret && entry.th32ProcessID != id)
{
ret = Process32Next(snapshot, &entry);
}
CloseHandle(snapshot);
return ret ? entry.cntThreads : 0;
#elif defined(__APPLE__)
const task_t task = mach_task_self();
mach_msg_type_number_t thread_count;
thread_act_array_t thread_list;
if (task_threads(task, &thread_list, &thread_count) != KERN_SUCCESS)
{
return 0;
}
vm_deallocate(task, reinterpret_cast<vm_address_t>(thread_list),
sizeof(thread_t) * thread_count);
return static_cast<u32>(thread_count);
#elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)
int mib[] = {
CTL_KERN,
KERN_PROC,
KERN_PROC_PID,
getpid(),
#if defined(__NetBSD__)
sizeof(struct kinfo_proc),
1,
#endif
};
u_int miblen = sizeof(mib) / sizeof(mib[0]);
struct kinfo_proc info;
size_t size = sizeof(info);
if (sysctl(mib, miblen, &info, &size, NULL, 0))
{
return 0;
}
return KP_NLWP(info);
#elif defined(__OpenBSD__)
int mib[] = {
CTL_KERN,
KERN_PROC,
KERN_PROC_PID | KERN_PROC_SHOW_THREADS,
getpid(),
sizeof(struct kinfo_proc),
0,
};
u_int miblen = sizeof(mib) / sizeof(mib[0]);

// get number of structs
size_t size;
if (sysctl(mib, miblen, NULL, &size, NULL, 0))
{
return 0;
}
mib[5] = size / mib[4];

// populate array of structs
struct kinfo_proc info[mib[5]];
if (sysctl(mib, miblen, &info, &size, NULL, 0))
{
return 0;
}

// exclude empty members
u32 thread_count{0};
for (int i = 0; i < size / mib[4]; i++)
{
if (info[i].p_tid != -1)
++thread_count;
}
return thread_count;
#elif defined(__linux__)
u32 thread_count{0};

DIR* proc_dir = opendir("/proc/self/task");
if (proc_dir)
{
// proc available, iterate through tasks and count them
struct dirent* entry;
while ((entry = readdir(proc_dir)) != NULL)
{
if (entry->d_name[0] == '.')
continue;

++thread_count;
}

closedir(proc_dir);
}
return thread_count;
#else
// unimplemented
return 0;
#endif
}
};
31 changes: 31 additions & 0 deletions Utilities/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <pthread.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <time.h>
#endif

#include "sync.h"
Expand Down Expand Up @@ -1851,6 +1852,36 @@ void thread_ctrl::notify()
}
}

u64 thread_ctrl::get_cycles()
{
u64 cycles;

#ifdef _WIN32
if (QueryThreadCycleTime((HANDLE)m_thread.load(), &cycles))
{
#else
struct timespec thread_time;
if (!clock_gettime(CLOCK_THREAD_CPUTIME_ID, &thread_time))
{
cycles = static_cast<u64>(thread_time.tv_sec) * 1'000'000'000 + thread_time.tv_nsec;
#endif
// Report 0 the first time this function is called
if (m_cycles == 0)
{
m_cycles = cycles;
return 0;
}

const auto diff_cycles = cycles - m_cycles;
m_cycles = cycles;
return diff_cycles;
}
else
{
return m_cycles;
}
}

void thread_ctrl::test()
{
const auto _this = g_tls_this_thread;
Expand Down
12 changes: 12 additions & 0 deletions Utilities/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class thread_ctrl final
// Fixed name
std::string m_name;

// CPU cycles thread has run for
u64 m_cycles{0};

// Start thread
static void start(const std::shared_ptr<thread_ctrl>&, task_stack);

Expand Down Expand Up @@ -172,6 +175,15 @@ class thread_ctrl final
return m_name;
}

// Get CPU cycles since last time this function was called. First call returns 0.
u64 get_cycles();

// Get platform-specific thread handle
std::uintptr_t get_native_handle() const
{
return m_thread.load();
}

// Get exception
std::exception_ptr get_exception() const;

Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/RSX/GL/GLOverlays.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ namespace gl
}
}

void run(u16 w, u16 h, GLuint target, rsx::overlays::user_interface& ui)
void run(u16 w, u16 h, GLuint target, rsx::overlays::overlay& ui)
{
program_handle.uniforms["ui_scale"] = color4f((f32)ui.virtual_width, (f32)ui.virtual_height, 1.f, 1.f);
program_handle.uniforms["time"] = (f32)(get_system_time() / 1000) * 0.005f;
Expand Down
Loading