Skip to content

Refactor/11850 Flow_threads refactor: unify recycler and manager computation v3 - #42

Closed
KEIAHNY wants to merge 1 commit into
mainfrom
11850-refactor-flow_threads-v3
Closed

KEIAHNY wants to merge 1 commit into
mainfrom
11850-refactor-flow_threads-v3

Conversation

@KEIAHNY

@KEIAHNY KEIAHNY commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

PR Type

Enhancement


Description

  • Extract shared get_up_windows window-detection logic

  • Add get_window_cpu_average_vector CPU averaging helper

  • Reuse unified logic in recycler and manager computations

  • Remove resolved duplication issue from ISSUES.md


Diagram Walkthrough

flowchart LR
  A["get_recycler_count"] -- "calls" --> C["get_up_windows"]
  B["get_manager_count"] -- "calls" --> C
  C -- "uses" --> D["get_window_cpu_average_vector"]
Loading

File Walkthrough

Relevant files
Enhancement
flow_threads.rs
Unify recycler and manager window computation                       

src/flow_threads.rs

  • Extract duplicated window-scanning loop into generic get_up_windows
    with threshold and above parameters
  • Add get_window_cpu_average_vector helper for per-window CPU usage
    averaging
  • Refactor get_recycler_count and manager computation to consume the
    shared function
  • Reset up flag each iteration and use WINDOWS instead of hardcoded 3 in
    tail check
+66/-103
Documentation
ISSUES.md
Remove resolved code duplication issue                                     

ISSUES.md

  • Remove resolved issue about duplicated recycler/manager mechanism
+0/-1     

@KEIAHNY KEIAHNY self-assigned this Oct 12, 2026
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Dropped CPU check

The old manager computation included a second gate: if avg > CPU_USAGE_MAX { up &= true; } else { up &= false; break; }. The unified get_up_windows only checks the slope threshold, so the manager path no longer requires CPU usage above CPU_USAGE_MAX for a window to count as "up". This silently changes get_manager_count behavior: windows that were previously rejected due to low manager CPU usage are now accepted, which can produce a lower (and incorrect) mgr_power minimum. In the new code the computed avg is also now unused, which corroborates that the check was lost rather than intentionally relocated.

for current_window in &current_windows {
    let avg: f32 = avg_cpu_usage[(sm_counter * (avg_cpu_usage.len()/WINDOWS as usize)) ..
        ((sm_counter + 1) * (avg_cpu_usage.len()/WINDOWS as usize))]
        .iter().sum::<f32>() / (avg_cpu_usage.len()/WINDOWS as usize) as f32;
    sm_counter+=1;

    if (above && current_window > &threshold) || (!above && current_window < &threshold) {
        tmp_slope_vector.push(*current_window);
        up &= true;
    }
    else {
        up &= false;

@github-actions

Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Avoid out-of-bounds panic when slicing windows

This slice panics if result.len() is not an exact multiple of WINDOWS, or if the
float-equality-based break condition never matches before counter walks past the end
of result. Use a bounds-checked slice and break cleanly when the data is exhausted,
turning a potential panic into graceful termination.

src/flow_threads.rs [86]

-            current_windows = result[(counter*WINDOWS) as usize..((counter+1)*WINDOWS) as usize].to_vec();
+            let start = (counter * WINDOWS) as usize;
+            if start >= result.len() {
+                break;
+            }
+            let end = (((counter + 1) * WINDOWS) as usize).min(result.len());
+            current_windows = result[start..end].to_vec();
Suggestion importance[1-10]: 7

__

Why: This addresses a real panic risk: the slice result[(counter*WINDOWS)..((counter+1)*WINDOWS)] will panic if result.len() is not an exact multiple of WINDOWS before the break condition triggers. The bounds-checked slice with a clean break converts a hard panic into graceful termination, matching the PR's refactored get_up_windows logic. Slightly reduced because the partial-window case could desync with the full-window avg_cpu_usage indexing, but the core fix is valid and important.

Medium
Prevent NaN from empty slope vectors

If current_windows is empty (e.g., when result is exhausted but the break condition
hasn't triggered), the inner for loop never runs, up remains true, and an empty
tmp_slope_vector is pushed. Downstream, dividing by tmp_slope_vector.len() as f64
yields NaN, and the later partial_cmp(...).expect(...) calls in
get_recycler_count/get_manager_count will panic because NaN comparisons return None.
Guard against empty windows before pushing.

src/flow_threads.rs [113-115]

-            if up {
+            if up && !tmp_slope_vector.is_empty() {
                 up_windows.push((counter, tmp_slope_vector.clone()));
             }
Suggestion importance[1-10]: 3

__

Why: The downstream NaN/panic chain described (empty tmp_slope_vector producing NaN, then partial_cmp failing) is logically accurate, but an empty current_windows cannot actually occur here: result[(counter*WINDOWS)..((counter+1)*WINDOWS)] would panic on out-of-bounds rather than return an empty slice, so an empty tmp_slope_vector with up == true is effectively unreachable. The guard is a harmless defensive improvement with low practical impact.

Low
  • Author self-review: I have reviewed the PR code suggestions, and addressed the relevant ones.

@KEIAHNY

KEIAHNY commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator Author

Closing - applying changes.

@KEIAHNY KEIAHNY closed this Oct 13, 2026
@KEIAHNY
KEIAHNY deleted the 11850-refactor-flow_threads-v3 branch October 13, 2026 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant