Skip to content

Comments

Single cpu affinity(UBLK_DEV_F_SINGLE_CPU_AFFINITY)#22

Merged
ming1 merged 3 commits intomainfrom
single-cpu-affinity
Aug 17, 2025
Merged

Single cpu affinity(UBLK_DEV_F_SINGLE_CPU_AFFINITY)#22
ming1 merged 3 commits intomainfrom
single-cpu-affinity

Conversation

@ming1
Copy link
Collaborator

@ming1 ming1 commented Aug 17, 2025

  • add feature UBLK_DEV_F_SINGLE_CPU_AFFINITY for just selecting one cpu as queue thread's sched affinity

ming1 added 3 commits August 17, 2025 15:28
1. Added new flag to UblkFlags
  - enable single CPU affinity optimization: select one random CPU
  - from queue's affinity instead of setting all CPUs const
    UBLK_DEV_F_SINGLE_CPU_AFFINITY = 0b00010000;

2. Modified create_queue_handlers()
  - Added conditional logic to check for the UBLK_DEV_F_SINGLE_CPU_AFFINITY flag
  - Only applies random CPU selection and single CPU affinity when the flag is set
  - Falls back to original behavior (full affinity) when flag is not set
  - Only stores selected CPU information when single CPU mode is enabled

3. Modified build_json()
  - Added conditional logic to check for the UBLK_DEV_F_SINGLE_CPU_AFFINITY flag
  - Uses stored single CPU affinity when flag is set
  - Calls get_queue_affinity() to get full affinity when flag is not set
  - Ensures JSON output reflects the actual affinity used by threads

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Test Coverage:

1. Flag Definition and Usage:
  - Verifies the flag can be combined with other flags using bitwise OR
  - Tests that flag detection works correctly with contains()
2. Control Device Integration:
  - Creates devices with and without the flag
  - Verifies the flag is properly stored and retrieved via get_dev_flags()
3. UblkQueueAffinity Helper Methods:
  - Tests from_single_cpu() method to create single-CPU affinity
  - Verifies to_bits_vec() returns correct CPU list
  - Tests set_cpu() method for building multi-CPU affinity
4. Random CPU Selection:
  - Creates a multi-CPU affinity (CPUs 1, 3, 5)
  - Tests get_random_cpu() method returns a valid CPU from the set
  - Verifies the selection is one of the expected CPUs

Test Output:

The test produces informative output showing:
✓ Single CPU affinity feature tests passed
  - Flag definition and usage: PASS
  - Control device flag storage: PASS
  - Single CPU affinity creation: PASS
  - Random CPU selection: PASS (selected CPU 5)

Key Benefits:

1. Comprehensive Coverage: Tests both the flag mechanism and the underlying affinity functionality
2. Fast Execution: Doesn't require actual device operation, so it runs quickly
3. Deterministic: Tests concrete functionality rather than relying on system behavior
4. Informative: Provides clear feedback about what was tested and the results

The test verifies that:
- The new flag is properly defined and can be used
- Control devices correctly store and retrieve the flag
- Single CPU affinity creation works as expected
- Random CPU selection from multi-CPU affinity works correctly

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Function: test_ublk_single_cpu_affinity()

What it tests:
1. Device Creation: Creates a ublk device with UBLK_DEV_F_SINGLE_CPU_AFFINITY flag
2. JSON Verification: Reads the exported JSON file and verifies queue affinity configuration
3. Single CPU Constraint: Ensures each queue has exactly one CPU in its affinity
4. Device Functionality: Verifies the device works normally with disk I/O operations

Key Features:
- Real Integration Test: Actually creates and runs a ublk device, not just unit testing
- JSON File Analysis: Reads the real JSON configuration file that ublk exports
- Concrete Verification: Checks that each queue's affinity array contains exactly one CPU
- Informative Output: Shows which CPU each queue is bound to

Test Structure:

1. Flag Setup: Creates device with UblkFlags::UBLK_DEV_F_ADD_DEV | UblkFlags::UBLK_DEV_F_SINGLE_CPU_AFFINITY
2. Device Operation: Runs a functional null device that can handle I/O
3. Verification Functions:
  - verify_single_cpu_affinity(): Reads JSON and validates affinity
  - single_cpu_null_handle_queue(): Handles device I/O operations
4. Comprehensive Testing: Runs sanity tests, affinity verification, and I/O tests

Benefits:

- End-to-End Testing: Tests the complete feature from flag setting to actual CPU binding
- Real-World Validation: Uses actual device creation and JSON export mechanisms
- Regression Prevention: Ensures the feature continues working as the codebase evolves
- Clear Diagnostics: Provides informative output showing actual CPU assignments

The test successfully demonstrates that:
✅ Devices can be created with the UBLK_DEV_F_SINGLE_CPU_AFFINITY flag
✅ Each queue gets bound to exactly one CPU (not multiple CPUs)
✅ The feature works with real device operation and I/O handling
✅ The JSON export correctly reflects the single CPU affinity

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
@ming1 ming1 merged commit 0349fe5 into main Aug 17, 2025
8 checks passed
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.

1 participant