affinity.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * ReactOS Task Manager
  3. *
  4. * affinity.c
  5. *
  6. * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
  7. * Copyright (C) 2008 Vladimir Pankratov
  8. * Copyright (C) 2019 Isira Seneviratne
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <windows.h>
  27. #include <commctrl.h>
  28. #include <winnt.h>
  29. #include "taskmgr.h"
  30. #include "perfdata.h"
  31. HANDLE hProcessAffinityHandle;
  32. WCHAR wszUnable2Access[255];
  33. static INT_PTR CALLBACK
  34. AffinityDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  35. {
  36. DWORD_PTR dwProcessAffinityMask = 0;
  37. DWORD_PTR dwSystemAffinityMask = 0;
  38. WCHAR wstrErrorText[256];
  39. int i;
  40. switch (message) {
  41. case WM_INITDIALOG:
  42. /*
  43. * Get the current affinity mask for the process and
  44. * the number of CPUs present in the system
  45. */
  46. if (!GetProcessAffinityMask(hProcessAffinityHandle, &dwProcessAffinityMask, &dwSystemAffinityMask)) {
  47. GetLastErrorText(wstrErrorText, ARRAY_SIZE(wstrErrorText));
  48. EndDialog(hDlg, 0);
  49. LoadStringW(hInst, IDS_AFFINITY_UNABLE2ACCESS, wszUnable2Access, ARRAY_SIZE(wszUnable2Access));
  50. MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Access, MB_OK|MB_ICONSTOP);
  51. }
  52. /*
  53. * Enable a checkbox for each processor present in the system
  54. */
  55. for (i = 0; i < 32; i++)
  56. if (dwSystemAffinityMask & (1 << i))
  57. EnableWindow(GetDlgItem(hDlg, IDC_CPU0 + i), TRUE);
  58. /*
  59. * Check each checkbox that the current process
  60. * has affinity with
  61. */
  62. for (i = 0; i < 32; i++)
  63. if (dwProcessAffinityMask & (1 << i))
  64. SendMessageW(GetDlgItem(hDlg, IDC_CPU0 + i), BM_SETCHECK, BST_CHECKED, 0);
  65. return TRUE;
  66. case WM_COMMAND:
  67. /*
  68. * If the user has cancelled the dialog box
  69. * then just close it
  70. */
  71. if (LOWORD(wParam) == IDCANCEL) {
  72. EndDialog(hDlg, LOWORD(wParam));
  73. return TRUE;
  74. }
  75. /*
  76. * The user has clicked OK -- so now we have
  77. * to adjust the process affinity mask
  78. */
  79. if (LOWORD(wParam) == IDOK) {
  80. /*
  81. * First we have to create a mask out of each
  82. * checkbox that the user checked.
  83. */
  84. for (i = 0; i < 32; i++)
  85. if (SendMessageW(GetDlgItem(hDlg, IDC_CPU0 + i), BM_GETCHECK, 0, 0))
  86. dwProcessAffinityMask |= (1 << i);
  87. /*
  88. * Make sure they are giving the process affinity
  89. * with at least one processor. I'd hate to see a
  90. * process that is not in a wait state get deprived
  91. * of its cpu time.
  92. */
  93. if (!dwProcessAffinityMask) {
  94. WCHAR wszErrorMsg[255];
  95. WCHAR wszErrorTitle[255];
  96. LoadStringW(hInst, IDS_AFFINITY_ERROR_MESSAGE, wszErrorMsg, ARRAY_SIZE(wszErrorMsg));
  97. LoadStringW(hInst, IDS_AFFINITY_ERROR_TITLE, wszErrorTitle, ARRAY_SIZE(wszErrorTitle));
  98. MessageBoxW(hDlg, wszErrorMsg, wszErrorTitle, MB_OK|MB_ICONSTOP);
  99. return TRUE;
  100. }
  101. /*
  102. * Try to set the process affinity
  103. */
  104. if (!SetProcessAffinityMask(hProcessAffinityHandle, dwProcessAffinityMask)) {
  105. GetLastErrorText(wstrErrorText, ARRAY_SIZE(wstrErrorText));
  106. EndDialog(hDlg, LOWORD(wParam));
  107. LoadStringW(hInst, IDS_AFFINITY_UNABLE2ACCESS, wszUnable2Access, ARRAY_SIZE(wszUnable2Access));
  108. MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Access, MB_OK|MB_ICONSTOP);
  109. }
  110. EndDialog(hDlg, LOWORD(wParam));
  111. return TRUE;
  112. }
  113. break;
  114. }
  115. return 0;
  116. }
  117. void ProcessPage_OnSetAffinity(void)
  118. {
  119. LV_ITEMW lvitem;
  120. ULONG Index, Count;
  121. DWORD dwProcessId;
  122. WCHAR wstrErrorText[256];
  123. Count = SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0);
  124. for (Index=0; Index<Count; Index++) {
  125. memset(&lvitem, 0, sizeof(LV_ITEMW));
  126. lvitem.mask = LVIF_STATE;
  127. lvitem.stateMask = LVIS_SELECTED;
  128. lvitem.iItem = Index;
  129. SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem);
  130. if (lvitem.state & LVIS_SELECTED)
  131. break;
  132. }
  133. Count = SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0);
  134. dwProcessId = PerfDataGetProcessId(Index);
  135. if ((Count != 1) || (dwProcessId == 0))
  136. return;
  137. hProcessAffinityHandle = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION, FALSE, dwProcessId);
  138. if (!hProcessAffinityHandle) {
  139. GetLastErrorText(wstrErrorText, ARRAY_SIZE(wstrErrorText));
  140. LoadStringW(hInst, IDS_AFFINITY_UNABLE2ACCESS, wszUnable2Access, ARRAY_SIZE(wszUnable2Access));
  141. MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Access, MB_OK|MB_ICONSTOP);
  142. return;
  143. }
  144. DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_AFFINITY_DIALOG), hMainWnd, AffinityDialogWndProc);
  145. if (hProcessAffinityHandle) {
  146. CloseHandle(hProcessAffinityHandle);
  147. hProcessAffinityHandle = NULL;
  148. }
  149. }