private RowAggregateData GetGroupAggregateData(string appName, AppResourceGroupInfo group, ulong totalProcessPrivateCommit) { Debug.WriteLine("GetGroupAggregateData: app: {0}, group: {1}", appName, group.InstanceId); ulong commitLimit = 0; ulong totalCommit = 0; ulong privateCommit = 0; ExecutionStateEx executionState = ExecutionStateEx.Unknown; EnergyQuotaStateEx energyState = EnergyQuotaStateEx.Unknown; int bgTaskCount = 0; try { AppResourceGroupMemoryReport mReport = group.GetMemoryReport(); if (mReport != null) { commitLimit = mReport.CommitUsageLimit; totalCommit = mReport.TotalCommitUsage; privateCommit = mReport.PrivateCommitUsage; } } catch (Exception ex) { Debug.WriteLine("GetMemoryReport: " + ex.ToString()); } // The resource group Private Commit should always be the same (or almost the same) as the PDI Private Commit. Debug.WriteLine("Private Commit: ResourceGroup={0:N0} bytes ~= Total PDI={1:N0} bytes", privateCommit, totalProcessPrivateCommit); try { AppResourceGroupStateReport sReport = group.GetStateReport(); if (sReport != null) { executionState = (ExecutionStateEx)sReport.ExecutionState; energyState = (EnergyQuotaStateEx)sReport.EnergyQuotaState; } } catch (Exception ex) { Debug.WriteLine("GetStateReport: " + ex.ToString()); } try { IList <AppResourceGroupBackgroundTaskReport> bgReports = group.GetBackgroundTaskReports(); if (bgReports != null) { bgTaskCount = bgReports.Count; } } catch (Exception ex) { Debug.WriteLine("GetBackgroundTaskReports: " + ex.ToString()); } RowAggregateData groupData = new RowAggregateData( commitLimit, totalCommit, privateCommit, bgTaskCount, executionState, energyState); return(groupData); }
private bool AddOrUpdateApp( AppDiagnosticInfo app, RowAggregateData appData, ExecutionStateEx appExecutionState) { bool result = false; string name = "(unknown)"; string appId = string.Empty; if (app.AppInfo != null && app.AppInfo.DisplayInfo != null) { appId = app.AppInfo.AppUserModelId; name = app.AppInfo.DisplayInfo.DisplayName; } // Check to see if this app is already in our list, and only add it if it wasn't already there. bool isAppFound = false; foreach (AppRowInfo existingRow in this) { if (appId == existingRow.Id) { isAppFound = true; break; } } // Don't add apps for resource groups that are not running. // Otherwise, for one thing, trying to get the logo would throw. if (!isAppFound && appExecutionState != ExecutionStateEx.NotRunning) { BitmapImage logo = GetLogoFromAppInfo(app); // A new app has appeared, so we add it to the list. AppRowInfo appRow = new AppRowInfo( app, appId, logo, name, appData.CommitLimit, appData.TotalCommit, appData.PrivateCommit, appData.ExecutionState, appData.EnergyState, appData.BgTaskCount); Add(appRow); result = true; } else { // For existing rows, we'll update the dynamic state. IEnumerable <AppRowInfo> appRows = this.Where(r => r.Id == appId); if (appRows != null && appRows.Count() > 0) { AppRowInfo existingApp = appRows.First(); existingApp.Update( appData.CommitLimit, appData.TotalCommit, appData.PrivateCommit, appData.ExecutionState, appData.EnergyState, appData.BgTaskCount); } } return(result); }
public bool Update() { bool result = false; // If we don't lock the collection, there's a race where databinding could end up with // additional spurious instances, especially if the polling interval is only 1-2 sec. lock (lockObject) { // Get a list of running apps (filtered by permission level). IAsyncOperation <IList <AppDiagnosticInfo> > infoOperation = AppDiagnosticInfo.RequestInfoAsync(); Task <IList <AppDiagnosticInfo> > infoTask = infoOperation.AsTask(); infoTask.Wait(); IList <AppDiagnosticInfo> runningApps = infoTask.Result; // We don't recreate the list each time: instead, we remove dead rows and update existing ones, // only adding new rows for new apps that appear. This is slow, but typically we'd only have a small // number of UWP apps running (say 5-20), so it's not noticeable. if (runningApps != null) { // First, remove from the list any apps that are no longer in memory. RemoveDeadRows(runningApps); // Find key data from the bottom-up (process and group level) for roll-up purposes. foreach (AppDiagnosticInfo app in runningApps) { ulong appCommitLimit = 0; ulong appTotalCommit = 0; ulong appPrivateCommit = 0; ExecutionStateEx appExecutionState = ExecutionStateEx.Unknown; EnergyQuotaStateEx appEnergyState = EnergyQuotaStateEx.Unknown; int appBgTaskCount = 0; // Get the roll-up data from group and process level. IList <AppResourceGroupInfo> groups = app.GetResourceGroups(); if (groups != null) { foreach (AppResourceGroupInfo group in groups) { Debug.WriteLine("app: {0}, group Id: {1}", app.AppInfo.DisplayInfo.DisplayName, group.InstanceId); // Get the total private commit usage of all processes for this app. ulong totalProcessPrivateCommit = 0; IList <ProcessDiagnosticInfo> processes = group.GetProcessDiagnosticInfos(); if (processes != null) { foreach (ProcessDiagnosticInfo process in processes) { totalProcessPrivateCommit += GetProcessPrivateCommit(process); } } // Accumulate the aggregated totals for all resource groups for this app. // We pass down the private commit for all processes for this group for comparison only. RowAggregateData groupData = GetGroupAggregateData( app.AppInfo.DisplayInfo.DisplayName, group, totalProcessPrivateCommit); appCommitLimit += groupData.CommitLimit; appTotalCommit += groupData.TotalCommit; appPrivateCommit += groupData.PrivateCommit; appBgTaskCount += groupData.BgTaskCount; if (appExecutionState != groupData.ExecutionState) { if (appExecutionState == ExecutionStateEx.Unknown) { appExecutionState = groupData.ExecutionState; } else { appExecutionState = ExecutionStateEx.Multiple; } } if (appEnergyState != groupData.EnergyState) { if (appEnergyState == EnergyQuotaStateEx.Unknown) { appEnergyState = groupData.EnergyState; } else { appEnergyState = EnergyQuotaStateEx.Multiple; } } } } RowAggregateData appData = new RowAggregateData( appCommitLimit, appTotalCommit, appPrivateCommit, appBgTaskCount, appExecutionState, appEnergyState); // Now add or update the rows. bool isAppAdded = AddOrUpdateApp(app, appData, appExecutionState); if (isAppAdded) { // If any row update resulted in adding an app, we set the return value to true. result = true; } } } } return(result); }