|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace VirtualClient.Common.ProcessAffinity |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Linq; |
| 8 | + using System.Text.RegularExpressions; |
| 9 | + using NUnit.Framework; |
| 10 | + |
| 11 | + [TestFixture] |
| 12 | + [Category("Unit")] |
| 13 | + public class LinuxProcessAffinityConfigurationTests |
| 14 | + { |
| 15 | + [Test] |
| 16 | + public void LinuxProcessAffinityConfigurationGeneratesCorrectNumactlSpecForSingleCore() |
| 17 | + { |
| 18 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration(new[] { 0 }); |
| 19 | + |
| 20 | + // Verify through GetCommandWithAffinity which uses GetNumactlCoreSpec internally |
| 21 | + string command = config.GetCommandWithAffinity("test", null); |
| 22 | + |
| 23 | + Assert.IsTrue(command.Contains("-C 0")); |
| 24 | + } |
| 25 | + |
| 26 | + [Test] |
| 27 | + public void LinuxProcessAffinityConfigurationGeneratesCorrectNumactlSpecForContiguousCores() |
| 28 | + { |
| 29 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration(new[] { 0, 1, 2, 3 }); |
| 30 | + |
| 31 | + string command = config.GetCommandWithAffinity("test", null); |
| 32 | + |
| 33 | + // Should be optimized to range notation |
| 34 | + Assert.IsTrue(command.Contains("-C 0-3")); |
| 35 | + } |
| 36 | + |
| 37 | + [Test] |
| 38 | + public void LinuxProcessAffinityConfigurationGeneratesCorrectNumactlSpecForNonContiguousCores() |
| 39 | + { |
| 40 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration(new[] { 0, 2, 4 }); |
| 41 | + |
| 42 | + string command = config.GetCommandWithAffinity("test", null); |
| 43 | + |
| 44 | + Assert.IsTrue(command.Contains("-C 0,2,4")); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public void LinuxProcessAffinityConfigurationGeneratesCorrectNumactlSpecForMixedCores() |
| 49 | + { |
| 50 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration(new[] { 0, 1, 2, 5, 7, 8, 9 }); |
| 51 | + |
| 52 | + string command = config.GetCommandWithAffinity("test", null); |
| 53 | + |
| 54 | + // Should optimize ranges: 0-2,5,7-9 |
| 55 | + Assert.IsTrue(command.Contains("-C 0-2,5,7-9")); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public void LinuxProcessAffinityConfigurationGeneratesCorrectNumactlSpecForComplexPattern() |
| 60 | + { |
| 61 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration( |
| 62 | + new[] { 0, 1, 2, 5, 6, 10, 12, 13, 14, 15 }); |
| 63 | + |
| 64 | + string command = config.GetCommandWithAffinity("test", null); |
| 65 | + |
| 66 | + // 0-2 (3 cores), 5,6 (2 cores), 10 (single), 12-15 (4 cores) |
| 67 | + Assert.IsTrue(command.Contains("-C 0-2,5,6,10,12-15")); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void LinuxProcessAffinityConfigurationGeneratesCorrectNumactlSpecForHighCoreIndices() |
| 72 | + { |
| 73 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration(new[] { 100, 101, 102 }); |
| 74 | + |
| 75 | + string command = config.GetCommandWithAffinity("test", null); |
| 76 | + |
| 77 | + Assert.IsTrue(command.Contains("-C 100-102")); |
| 78 | + } |
| 79 | + |
| 80 | + [Test] |
| 81 | + public void LinuxProcessAffinityConfigurationGeneratesCorrectNumactlCommandForSingleCore() |
| 82 | + { |
| 83 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration(new[] { 0 }); |
| 84 | + |
| 85 | + string command = config.GetCommandWithAffinity(null, "myworkload --arg1 --arg2"); |
| 86 | + |
| 87 | + Assert.AreEqual("\"numactl -C 0 myworkload --arg1 --arg2\"", command); |
| 88 | + } |
| 89 | + |
| 90 | + [Test] |
| 91 | + public void LinuxProcessAffinityConfigurationGeneratesCorrectNumactlCommandForMultipleCores() |
| 92 | + { |
| 93 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration(new[] { 0, 1, 2 }); |
| 94 | + |
| 95 | + string command = config.GetCommandWithAffinity(null, "myworkload --arg1 --arg2"); |
| 96 | + |
| 97 | + Assert.AreEqual("\"numactl -C 0-2 myworkload --arg1 --arg2\"", command); |
| 98 | + } |
| 99 | + |
| 100 | + [Test] |
| 101 | + public void LinuxProcessAffinityConfigurationGeneratesCorrectNumactlCommandWithEmptyArguments() |
| 102 | + { |
| 103 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration(new[] { 1, 3, 5 }); |
| 104 | + |
| 105 | + string command = config.GetCommandWithAffinity(null, "myworkload"); |
| 106 | + |
| 107 | + Assert.AreEqual("\"numactl -C 1,3,5 myworkload\"", command); |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public void LinuxProcessAffinityConfigurationHandlesComplexArguments() |
| 112 | + { |
| 113 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration(new[] { 0, 1 }); |
| 114 | + |
| 115 | + string command = config.GetCommandWithAffinity( |
| 116 | + null, |
| 117 | + "myworkload --file=\"path with spaces\" --option=value"); |
| 118 | + |
| 119 | + // 2 cores use comma notation (0,1), not range (0-1) |
| 120 | + Assert.AreEqual( |
| 121 | + "\"numactl -C 0,1 myworkload --file=\"path with spaces\" --option=value\"", |
| 122 | + command); |
| 123 | + } |
| 124 | + |
| 125 | + [Test] |
| 126 | + public void LinuxProcessAffinityConfigurationToStringIncludesNumactlSpec() |
| 127 | + { |
| 128 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration(new[] { 0, 1, 2, 5 }); |
| 129 | + |
| 130 | + string result = config.ToString(); |
| 131 | + |
| 132 | + Assert.IsTrue(result.Contains("0,1,2,5")); |
| 133 | + Assert.IsTrue(result.Contains("numactl: -C 0-2,5")); |
| 134 | + } |
| 135 | + |
| 136 | + [Test] |
| 137 | + public void LinuxProcessAffinityConfigurationOptimizesRanges() |
| 138 | + { |
| 139 | + // Test various range optimization scenarios by checking the command output |
| 140 | + // Note: 2 consecutive cores use comma notation (0,1), 3+ use range notation (0-2) |
| 141 | + var testCases = new[] |
| 142 | + { |
| 143 | + (new[] { 0 }, "-C 0"), |
| 144 | + (new[] { 0, 1 }, "-C 0,1"), // 2 cores: comma notation |
| 145 | + (new[] { 0, 1, 2 }, "-C 0-2"), // 3+ cores: range notation |
| 146 | + (new[] { 0, 2 }, "-C 0,2"), |
| 147 | + (new[] { 0, 1, 3 }, "-C 0,1,3"), // 2 cores then gap |
| 148 | + (new[] { 0, 1, 2, 4, 5, 6 }, "-C 0-2,4-6"), // Two 3-core ranges |
| 149 | + (new[] { 0, 2, 4, 6, 8 }, "-C 0,2,4,6,8"), |
| 150 | + (new[] { 0, 1, 2, 3, 5, 6, 7, 8, 10 }, "-C 0-3,5-8,10") // 4-core range, 4-core range, single |
| 151 | + }; |
| 152 | + |
| 153 | + foreach (var (cores, expectedSpec) in testCases) |
| 154 | + { |
| 155 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration(cores); |
| 156 | + string command = config.GetCommandWithAffinity("test", null); |
| 157 | + Assert.IsTrue(command.Contains(expectedSpec), $"Failed for cores: {string.Join(",", cores)}. Expected '{expectedSpec}' in '{command}'"); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + [Test] |
| 162 | + public void LinuxProcessAffinityConfigurationHandlesUnsortedCores() |
| 163 | + { |
| 164 | + // Cores should be sorted before optimization |
| 165 | + LinuxProcessAffinityConfiguration config = new LinuxProcessAffinityConfiguration(new[] { 5, 0, 2, 1, 3 }); |
| 166 | + |
| 167 | + string command = config.GetCommandWithAffinity("test", null); |
| 168 | + |
| 169 | + // Should sort and optimize: 0-3,5 |
| 170 | + Assert.IsTrue(command.Contains("-C 0-3,5")); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments