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

Show thread name on threads list, if available. #60

Merged
merged 2 commits into from
Sep 2, 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
23 changes: 21 additions & 2 deletions src/profiler/threadinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,37 @@ static __int64 getTotal(FILETIME time)
return (__int64(time.dwHighDateTime) << 32) + time.dwLowDateTime;
}

typedef HRESULT( WINAPI *GetThreadDescriptionFunc )(HANDLE, PWSTR*);
static GetThreadDescriptionFunc GetThreadDescription = reinterpret_cast<GetThreadDescriptionFunc>(GetProcAddress( GetModuleHandle( TEXT( "Kernel32.dll" ) ), "GetThreadDescription" ));

bool hasThreadDescriptionAPI()
{
// Helper function to let main window decide whether to hide this column
return GetThreadDescription != NULL;
}

// DE: 20090325 Threads now have CPU usage
ThreadInfo::ThreadInfo(DWORD id_, HANDLE thread_handle_)
: id(id_), thread_handle(thread_handle_)
{
prevKernelTime.dwHighDateTime = prevKernelTime.dwLowDateTime = 0;
prevUserTime.dwHighDateTime = prevUserTime.dwLowDateTime = 0;
cpuUsage = -1;
cpuUsage = -1;

name = L"-";

// Try to use the new thread naming API from Win10 Creators update onwards if we have it
if (GetThreadDescription) {
PWSTR data;
HRESULT hr = GetThreadDescription(thread_handle, &data);
if (SUCCEEDED( hr )) {
if (wcslen(data) > 0)
name = data;
LocalFree(data);
}
}
}


ThreadInfo::~ThreadInfo()
{
}
Expand Down
3 changes: 3 additions & 0 deletions src/profiler/threadinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <windows.h>
#include <string>

bool hasThreadDescriptionAPI();

/*=====================================================================
ThreadInfo
Expand All @@ -52,6 +53,7 @@ class ThreadInfo
const std::wstring& getLocation() const { return location; }
void setLocation(const std::wstring &loc) { location = loc; }

const std::wstring& getName() const { return name; }
bool recalcUsage(int sampleTimeDiff);

FILETIME prevKernelTime, prevUserTime;
Expand All @@ -61,6 +63,7 @@ class ThreadInfo

private:
std::wstring location;
std::wstring name;
DWORD id;
HANDLE thread_handle;
};
Expand Down
30 changes: 29 additions & 1 deletion src/wxProfilerGUI/threadlist.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*=====================================================================
/*=====================================================================
threadList.cpp
---------------
File created by ClassTemplate on Sun Mar 20 17:33:43 2005
Expand Down Expand Up @@ -59,12 +59,21 @@ ThreadList::ThreadList(wxWindow *parent, const wxPoint& pos,
InsertColumn(COL_TOTALCPU, itemCol);
itemCol.m_text = _T("TID");
InsertColumn(COL_ID, itemCol);
itemCol.m_text = _T("Thread Name");
InsertColumn(COL_NAME, itemCol);

SetColumnWidth(COL_LOCATION, 220);
SetColumnWidth(COL_CPUUSAGE, 80);
SetColumnWidth(COL_TOTALCPU, 100);
SetColumnWidth(COL_ID, 60);

// We hide the thread name column if running it on an OS that doesn't
// support the API, to avoid wasting screen space.
if (hasThreadDescriptionAPI())
SetColumnWidth(COL_NAME, 150);
else
SetColumnWidth(COL_NAME, 0);

sort_column = COL_CPUUSAGE;
sort_dir = SORT_DOWN;
SetSortImage(sort_column, sort_dir);
Expand Down Expand Up @@ -155,6 +164,14 @@ struct IdDescPred { bool operator () (const ThreadInfo &a, const ThreadInfo &b)
return a.getID() > b.getID();
} };

struct NameAscPred { bool operator () (const ThreadInfo &a, const ThreadInfo &b) {
return a.getName() < b.getName();
} };

struct NameDescPred { bool operator () (const ThreadInfo &a, const ThreadInfo &b) {
return a.getName() > b.getName();
} };

void ThreadList::sortByLocation()
{
if (sort_dir == SORT_UP)
Expand Down Expand Up @@ -187,6 +204,14 @@ void ThreadList::sortByID()
std::stable_sort(threads.begin(), threads.end(), IdDescPred());
}

void ThreadList::sortByName()
{
if (sort_dir == SORT_UP)
std::stable_sort(threads.begin(), threads.end(), NameAscPred());
else
std::stable_sort(threads.begin(), threads.end(), NameDescPred());
}

void ThreadList::OnSort(wxListEvent& event)
{
SetSortImage(sort_column, SORT_NONE);
Expand All @@ -212,6 +237,7 @@ void ThreadList::updateSorting()
case COL_CPUUSAGE: sortByCpuUsage(); break;
case COL_TOTALCPU: sortByTotalCpuTime(); break;
case COL_ID: sortByID(); break;
case COL_NAME: sortByName(); break;
}
fillList();
}
Expand Down Expand Up @@ -245,6 +271,8 @@ void ThreadList::fillList()

sprintf(str, "%d", threads[i].getID());
this->SetItem(i, COL_ID, str);

this->SetItem(i, COL_NAME, threads[i].getName());
}
Thaw();
}
Expand Down
3 changes: 3 additions & 0 deletions src/wxProfilerGUI/threadlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class ThreadList : public wxSortedListCtrl
void sortByCpuUsage();
void sortByTotalCpuTime();
void sortByID();
void sortByName();

std::vector<const ThreadInfo*> getSelectedThreads(bool all=false);
private:
Expand All @@ -72,6 +73,7 @@ class ThreadList : public wxSortedListCtrl
COL_CPUUSAGE,
COL_TOTALCPU,
COL_ID,
COL_NAME,
NUM_COLUMNS
};

Expand All @@ -89,6 +91,7 @@ class ThreadList : public wxSortedListCtrl
void fillList();
int getNumDisplayedThreads();
std::wstring getLocation(HANDLE thread_handle);
std::wstring getName(HANDLE thread_handle);
};


Expand Down