Skip to content

[WIP] Integrate manage_prime_cores logic into apply_config function#2

Closed
Copilot wants to merge 1 commit intomasterfrom
copilot/integrate-manage-prime-cores
Closed

[WIP] Integrate manage_prime_cores logic into apply_config function#2
Copilot wants to merge 1 commit intomasterfrom
copilot/integrate-manage-prime-cores

Conversation

Copy link
Copy Markdown

Copilot AI commented Dec 2, 2025

Thanks for asking me to work on this. I will get started on it and keep this PR's description up to date as I form a plan and make progress.

Original prompt

Integrate manage_prime_cores logic into apply_config function in src/main.rs.

  1. Struct Definitions: Move ThreadStats and PrimeCoreScheduler definitions outside of main function (or where they were in the previous snippet) to the top level of the file, so they are accessible.
    • Ensure ThreadStats and PrimeCoreScheduler are defined.
    • Define Candidate struct used in the logic.
  2. Scheduler Instance: In main, create an instance of PrimeCoreScheduler (scheduler) and pass it to apply_config.
    • Update apply_config signature to accept &mut PrimeCoreScheduler.
    • Update apply_config call site in main.
  3. Reuse Process Affinity: In apply_config:
    • Define current_mask and system_mask variables before the affinity check block.
    • If config.affinity_mask != 0 OR config.prime_cpu_mask != 0, call GetProcessAffinityMask if it hasn't been called yet.
    • Use this current_mask for the S calculation logic (intersect prime_cpu_mask with current_mask or system_mask as discussed previously: S = PrimeMask & (ProcessAffinity if set else SystemAffinity)).
  4. Integrate Logic: Append the manage_prime_cores logic at the end of apply_config (after the cpu set mask block).
    • The logic should use the pid and scheduler passed to apply_config.
    • The logic involves:
      • Calculating S mask and prime_cpuset_ids.
      • Taking a thread snapshot (CreateToolhelp32Snapshot with TH32CS_SNAPTHREAD).
      • Iterating threads for the current pid.
      • Managing handles (open new, reuse existing VIP handle).
      • Querying cycle time (QueryThreadCycleTime).
      • Calculating delta and updating history.
      • Sorting candidates.
      • Applying CPU sets (SetThreadSelectedCpuSets) for VIPs.
      • Resetting CPU sets for demoted threads.
      • Closing handles for non-VIPs.
      • GC-ing dead threads from the scheduler.
  5. Struct Updates: Ensure ProcessConfig has the prime_cpu_mask field and read_config parses it (as per previous discussion).

Refinement on apply_config logic flow:

  • Open Process Handle h_prc.
  • ... (Priority setting) ...
  • Affinity Handling:
    • Initialize current_affinity_mask = 0, system_affinity_mask = 0.
    • Check if we need affinity: need_affinity = config.affinity_mask != 0 || config.prime_cpu_mask != 0.
    • If need_affinity:
      • Call GetProcessAffinityMask(h_prc, ...).
      • If successful, store results.
    • If config.affinity_mask != 0 and diff from current, SetProcessAffinityMask.
  • ... (Process Default CPU Set setting) ...
  • ... (IO Priority) ...
  • ... (Memory Priority) ...
  • Prime Core Management:
    • If config.prime_cpu_mask != 0:
      • Calculate effective S mask. (User mentioned calculating disjoint sets or intersection. The previous consensus was PrimeMask & (CurrentAffinity if valid else SystemAffinity)).
      • Execute the thread scanning, sorting, and setting logic using scheduler.
  • Close h_prc.

Note on Handle Reuse: The user mentioned "write in apply_config might directly utilize some existing HANDLE".

  • apply_config holds the Process Handle (h_prc).
  • manage_prime_cores needs Thread Handles.
  • h_prc is useful for GetProcessAffinityMask.
  • h_prc is not directly useful for opening threads (we still need OpenThread or Thread32First), but QueryThreadCycleTime takes a Thread Handle.
  • So the reuse is mainly about the affinity mask data.

Code Structure Plan:

  • Add ThreadStats, PrimeCoreScheduler structs at module level.
  • Add prime_cpu_mask to ProcessConfig.
  • Update read_config to parse the 7th column.
  • Modify apply_config to take scheduler.
  • Inside apply_config, implement the merged logic.
  • Modify main to init scheduler and call updated apply_config.

This pull request was created as a result of the following prompt from Copilot chat.

Integrate manage_prime_cores logic into apply_config function in src/main.rs.

  1. Struct Definitions: Move ThreadStats and PrimeCoreScheduler definitions outside of main function (or where they were in the previous snippet) to the top level of the file, so they are accessible.
    • Ensure ThreadStats and PrimeCoreScheduler are defined.
    • Define Candidate struct used in the logic.
  2. Scheduler Instance: In main, create an instance of PrimeCoreScheduler (scheduler) and pass it to apply_config.
    • Update apply_config signature to accept &mut PrimeCoreScheduler.
    • Update apply_config call site in main.
  3. Reuse Process Affinity: In apply_config:
    • Define current_mask and system_mask variables before the affinity check block.
    • If config.affinity_mask != 0 OR config.prime_cpu_mask != 0, call GetProcessAffinityMask if it hasn't been called yet.
    • Use this current_mask for the S calculation logic (intersect prime_cpu_mask with current_mask or system_mask as discussed previously: S = PrimeMask & (ProcessAffinity if set else SystemAffinity)).
  4. Integrate Logic: Append the manage_prime_cores logic at the end of apply_config (after the cpu set mask block).
    • The logic should use the pid and scheduler passed to apply_config.
    • The logic involves:
      • Calculating S mask and prime_cpuset_ids.
      • Taking a thread snapshot (CreateToolhelp32Snapshot with TH32CS_SNAPTHREAD).
      • Iterating threads for the current pid.
      • Managing handles (open new, reuse existing VIP handle).
      • Querying cycle time (QueryThreadCycleTime).
      • Calculating delta and updating history.
      • Sorting candidates.
      • Applying CPU sets (SetThreadSelectedCpuSets) for VIPs.
      • Resetting CPU sets for demoted threads.
      • Closing handles for non-VIPs.
      • GC-ing dead threads from the scheduler.
  5. Struct Updates: Ensure ProcessConfig has the prime_cpu_mask field and read_config parses it (as per previous discussion).

Refinement on apply_config logic flow:

  • Open Process Handle h_prc.
  • ... (Priority setting) ...
  • Affinity Handling:
    • Initialize current_affinity_mask = 0, system_affinity_mask = 0.
    • Check if we need affinity: need_affinity = config.affinity_mask != 0 || config.prime_cpu_mask != 0.
    • If need_affinity:
      • Call GetProcessAffinityMask(h_prc, ...).
      • If successful, store results.
    • If config.affinity_mask != 0 and diff from current, SetProcessAffinityMask.
  • ... (Process Default CPU Set setting) ...
  • ... (IO Priority) ...
  • ... (Memory Priority) ...
  • Prime Core Management:
    • If config.prime_cpu_mask != 0:
      • Calculate effective S mask. (User mentioned calculating disjoint sets or intersection. The previous consensus was PrimeMask & (CurrentAffinity if valid else SystemAffinity)).
      • Execute the thread scanning, sorting, and setting logic using scheduler.
  • Close h_prc.

Note on Handle Reuse: The user mentioned "write in apply_config might directly utilize some existing HANDLE".

  • apply_config holds the Process Handle (h_prc).
  • manage_prime_cores needs Thread Handles.
  • h_prc is useful for GetProcessAffinityMask.
  • h_prc is not directly useful for opening threads (we still need OpenThread or Thread32First), but QueryThreadCycleTime takes a Thread Handle.
  • So the reuse is mainly about the affinity mask data.

Code Structure Plan:

  • Add ThreadStats, PrimeCoreScheduler structs at module level.
  • Add prime_cpu_mask to ProcessConfig.
  • Update read_config to parse the 7th column.
  • Modify apply_config to take scheduler.
  • Inside apply_config, implement the merged logic.
  • Modify main to init scheduler and call updated apply_config.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Dec 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants