tôi đã làm việc đó - mặc dù nó không thực sự thẳng về phía trước.
trước hết tôi phải thực hiện một số chỉnh sửa cho các báo cáo trước đây của tôi - chỉ để ở cùng cấp.
Trong Dịch vụ đám mây có một số Máy ảo, mỗi máy chủ lưu trữ hoặc một WebRole Instance hoặc một WorkerRole Instance. Vì vậy, trên một máy ảo duy nhất chỉ có một w3wp duy nhất chạy hoặc không có w3wp ở tất cả, nhưng một quá trình waworkerhost.
Trong trường hợp đặc biệt của tôi, có khả năng có hai w3wp chạy trên một máy ảo duy nhất. vì vậy tôi cần phải phân biệt giữa hai lý do đó - do đó yêu cầu tôi thực hiện một số loại liên kết quá trình thể hiện.
Điều tôi muốn ghi là: Tổng tải CPU của một máy ảo duy nhất, tải CPU của quá trình diễn tiến chạy trên máy ảo (w3wp, waworkerhost).
PerformanceCounter cho Tổng tải CPU là dễ dàng và bình đẳng cho mỗi máy ảo: \ Processor (_Total) \% Processortime cho máy ảo webr i không thể sử dụng bộ xử lý \ process (w3wp) \% processortime vì tôi không thể chắc chắn nếu nó chính xác w3wp (xem ở trên)
Bây giờ ở đây là những gì tôi đã làm: Vì bạn phải bắt đầu một màn hình truy cập hiệu suất cho mỗi vai trò vai trò OnStart() trong WebRole.cs hoặc WorkerRole.cs tôi figured this là nơi duy nhất mà tôi có thể thu thập thông tin cần thiết bằng cách nào đó.
Trong WorkerRole.cs tôi đã làm:
int pc = Environment.ProcessorCount;
string instance = RoleEnvironment.CurrentRoleInstance.Id;
SomeOtherManagementClass.StartDiagnosticMonitorService(pc, instance, Process.GetCurrentProcess());
Trong WebRole.cs các CurrentProcess cũng trả WaWorkerHost, vì vậy tôi đã phải di chuyển codelines trên vào global.asax của WebRole. Ở đây Quy trình chính xác có sẵn.
Trong SomeOtherManagementClass tôi đặt StartDiagnosticsMonitorService, hiện nhận được CurrentProcess mà từ đó StartDiagnosticsMonitorService được gọi. (từ workerrole.cs nó sẽ nhận WaWorkerHost Quy trình và từ WebRoles quá trình w3wp - bao gồm PID)
public static void StartDiagnosticMonitorService(int coreCount, string currentRole, Process process)
{
string processName = GetProcessInstanceName(process.Id);
SetCPUCoreData(coreCount, currentRole, processName, process.Id);
...
instanceProcessLoadCounterName = String.Format(@"\Process({0})\% Processor Time", processName);
}
GetProcessInstanceName (process.Id) bây giờ được gọi là trên mỗi máy ảo và nhận được ProcessName đến process.Id cung cấp - Điều này cho phép bạn tạo sự khác biệt giữa nhiều w3wps trên một máy ảo duy nhất vì instanceNames được trả về là w3wp, w3wp # 1, w3wp # 2 vv. ngược lại với processName được cung cấp bởi GetCurrentProcess, đó là allways w3wp. i này sửa đổi một codesample tôi tìm thấy ở đây trên stackoverflow - bạn có thể tìm thấy nó dưới đây:
private static string GetProcessInstanceName(int pid)
{
PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
string[] instances = cat.GetInstanceNames();
foreach (string instance in instances)
{
try
{
using (PerformanceCounter cnt = new PerformanceCounter("Process",
"ID Process", instance, true))
{
int val = (int)cnt.RawValue;
if (val == pid)
{
return instance;
}
}
}
catch (InvalidOperationException)
{
//this point is reached when a process terminates while iterating the processlist- this it cannot be found
}
}
return "";
}
Cuối cùng nhưng không kém phần quan: SetCPUCoreData (coreCount, currentRole, ProcessName, process.Id) lưu tất cả dữ liệu liên quan của các quá trình để lưu trữ xanh nên nó có sẵn ở khắp mọi nơi từ trong ứng dụng:
private static void SetCPUCoreData(int count, string roleinstance, string processName, int processID)
{
string[] instances = roleinstance.Split('.');
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(GetSettingValue("LoadMonitor.Connection.String"));
CloudTableClient cloudTableClient = storageAccount.CreateCloudTableClient();
const string tableName = "PerformanceMonitorCoreCount";
cloudTableClient.CreateTableIfNotExist(tableName);
TableServiceContext serviceContext = cloudTableClient.GetDataServiceContext();
PerformanceCounterCPUCoreEntity ent = new PerformanceCounterCPUCoreEntity(count, instances[instances.Count() - 1],processName, processID);
serviceContext.AttachTo(tableName, ent);
serviceContext.UpdateObject(ent);
serviceContext.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate);
}
các PerformanceCounterCPUCoreEntity là một Template cho StorageTable - nhìn vào API lưu trữ xanh nếu bạn có bất cứ thắc mắc gì về phần này, hoặc chỉ cần hỏi.