-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoreMask.cs
More file actions
166 lines (143 loc) Β· 5.23 KB
/
Copy pathCoreMask.cs
File metadata and controls
166 lines (143 loc) Β· 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* ThreadPilot - Advanced Windows Process and Power Plan Manager
* Copyright (C) 2025 Prime Build
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace ThreadPilot.Models
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
/// <summary>
/// Represents a reusable CPU core affinity mask
/// Based on CPUSetSetter's LogicalProcessorMask.
/// </summary>
public partial class CoreMask : ObservableObject
{
[ObservableProperty]
private string id = Guid.NewGuid().ToString();
[ObservableProperty]
private string name = string.Empty;
[ObservableProperty]
private string description = string.Empty;
/// <summary>
/// Gets or sets array of boolean values, one per logical core.
/// </summary>
public ObservableCollection<bool> BoolMask { get; set; } = new();
public int ProfileSchemaVersion { get; set; } = CpuAffinityProfileSchemaVersions.Legacy;
public CpuSelection? CpuSelection { get; set; }
public CpuSelectionMigrationMetadata? CpuSelectionMigration { get; set; }
[ObservableProperty]
private bool isDefault = false;
[ObservableProperty]
private bool isEnabled = true;
[ObservableProperty]
private DateTime createdAt = DateTime.UtcNow;
[ObservableProperty]
private DateTime updatedAt = DateTime.UtcNow;
/// <summary>
/// Gets a value indicating whether special mask that allows all cores (no restrictions).
/// </summary>
public bool IsNoMask => this.BoolMask.All(b => b);
/// <summary>
/// Gets the count of selected cores.
/// </summary>
public int SelectedCoreCount => this.BoolMask.Count(b => b);
/// <summary>
/// Converts the boolean mask to a legacy 64-bit processor affinity value.
/// This is only safe for single processor-group selections below CPU 64;
/// topology-aware apply paths must prefer <see cref="CpuSelection"/>.
/// </summary>
public long ToProcessorAffinity()
{
long affinity = 0;
for (int i = 0; i < this.BoolMask.Count; i++)
{
if (this.BoolMask[i])
{
affinity |= 1L << i;
}
}
return affinity;
}
/// <summary>
/// Creates a CoreMask from a processor affinity value.
/// </summary>
public static CoreMask FromProcessorAffinity(long affinity, int coreCount, string name = "Custom")
{
var mask = new CoreMask { Name = name };
for (int i = 0; i < coreCount; i++)
{
mask.BoolMask.Add(((affinity >> i) & 1) == 1);
}
return mask;
}
/// <summary>
/// Creates a mask with all cores enabled.
/// </summary>
public static CoreMask CreateAllCoresMask(int coreCount)
{
var mask = new CoreMask
{
Name = "All Cores",
Description = "Use all available CPU cores",
IsDefault = true,
};
for (int i = 0; i < coreCount; i++)
{
mask.BoolMask.Add(true);
}
return mask;
}
/// <summary>
/// Creates a mask with no cores (empty mask, for deletion purposes).
/// </summary>
public static CoreMask CreateNoMask()
{
return new CoreMask
{
Name = "No Restriction",
Description = "Process can use all cores",
IsDefault = false,
};
}
public CoreMask Clone()
{
var cloned = new CoreMask
{
Id = Guid.NewGuid().ToString(),
Name = this.Name + " (Copy)",
Description = this.Description,
IsEnabled = this.IsEnabled,
IsDefault = false,
ProfileSchemaVersion = this.ProfileSchemaVersion,
CpuSelection = this.CpuSelection,
CpuSelectionMigration = this.CpuSelectionMigration,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
};
foreach (var bit in this.BoolMask)
{
cloned.BoolMask.Add(bit);
}
return cloned;
}
public override string ToString()
{
return $"{this.Name} ({this.SelectedCoreCount}/{this.BoolMask.Count} cores)";
}
}
}