Skip to content

Commit 2cee83b

Browse files
vitek-karasCopilot
andauthored
Add structured environment details to XHarness JSON results (#1599)
* Add structured environment info to JSON results Move host and target environment reporting into XHarness's structured JSON outputs for Android and Apple, including diagnostics and run-summary payloads. Co-authored-by: Copilot <[email protected]> * Address environment metadata review feedback Fix Android cpuinfo frequency fallback, avoid swallowing Apple cancellation, parse nested devicectl hardware details, and remove duplicate ADB version logging. Co-authored-by: Copilot <[email protected]> * Fix environment metadata edge cases Avoid process output deadlock, parse Android cpuinfo MHz with invariant culture, include environment data for android install diagnostics, and preserve devicectl cancellation/nested hardware metadata handling. Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 51ca379 commit 2cee83b

37 files changed

Lines changed: 1270 additions & 114 deletions

‎src/Microsoft.DotNet.XHarness.Android/AdbRunner.cs‎

Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
using System.Globalization;
89
using System.IO;
910
using System.Linq;
1011
using System.Runtime.InteropServices;
@@ -21,9 +22,18 @@ private enum AdbProperty
2122
{
2223
Architecture,
2324
ApiVersion,
25+
BuildFingerprint,
2426
SupportedArchitectures,
2527
InstalledApps,
2628
BootCompletion,
29+
CpuInfo,
30+
CpuMaxFrequency,
31+
CpuMaxFrequencyFallback,
32+
Manufacturer,
33+
MemInfo,
34+
Model,
35+
OperatingSystemVersion,
36+
ProductName,
2737
}
2838

2939
#region Constructor and state variables
@@ -33,8 +43,17 @@ private enum AdbProperty
3343
{ AdbProperty.SupportedArchitectures, new[] { "shell", "getprop", "ro.product.cpu.abilist" } },
3444
{ AdbProperty.ApiVersion, new[] { "shell", "getprop", "ro.build.version.sdk" } },
3545
{ AdbProperty.Architecture, new[] { "shell", "getprop", "ro.product.cpu.abi" } },
46+
{ AdbProperty.BuildFingerprint, new[] { "shell", "getprop", "ro.build.fingerprint" } },
3647
{ AdbProperty.InstalledApps, new[] { "shell", "pm", "list", "packages", "-3" } },
3748
{ AdbProperty.BootCompletion, new[] { "shell", "getprop", "sys.boot_completed" } },
49+
{ AdbProperty.CpuInfo, new[] { "shell", "cat", "/proc/cpuinfo" } },
50+
{ AdbProperty.CpuMaxFrequency, new[] { "shell", "cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" } },
51+
{ AdbProperty.CpuMaxFrequencyFallback, new[] { "shell", "cat", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq" } },
52+
{ AdbProperty.Manufacturer, new[] { "shell", "getprop", "ro.product.manufacturer" } },
53+
{ AdbProperty.MemInfo, new[] { "shell", "cat", "/proc/meminfo" } },
54+
{ AdbProperty.Model, new[] { "shell", "getprop", "ro.product.model" } },
55+
{ AdbProperty.OperatingSystemVersion, new[] { "shell", "getprop", "ro.build.version.release" } },
56+
{ AdbProperty.ProductName, new[] { "shell", "getprop", "ro.product.name" } },
3857
};
3958

4059
private const string AdbEnvironmentVariableName = "ADB_EXE_PATH";
@@ -92,6 +111,28 @@ public AdbRunner(ILogger log, IAdbProcessManager processManager, string adbExePa
92111
}
93112
}
94113

114+
public void PopulateEnvironmentInfo(AndroidDevice device)
115+
{
116+
if (device is null)
117+
{
118+
throw new ArgumentNullException(nameof(device));
119+
}
120+
121+
device.Manufacturer ??= GetDeviceProperty(AdbProperty.Manufacturer, device.DeviceSerial);
122+
device.Model ??= GetDeviceProperty(AdbProperty.Model, device.DeviceSerial);
123+
device.ProductName ??= GetDeviceProperty(AdbProperty.ProductName, device.DeviceSerial);
124+
device.OperatingSystemVersion ??= GetDeviceProperty(AdbProperty.OperatingSystemVersion, device.DeviceSerial);
125+
device.BuildFingerprint ??= GetDeviceProperty(AdbProperty.BuildFingerprint, device.DeviceSerial);
126+
device.Architecture ??= GetDeviceProperty(AdbProperty.Architecture, device.DeviceSerial);
127+
device.SupportedArchitectures ??= GetDeviceProperty(AdbProperty.SupportedArchitectures, device.DeviceSerial)?.Split(new[] { ',', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
128+
device.CpuModel ??= ParseCpuModel(GetDeviceProperty(AdbProperty.CpuInfo, device.DeviceSerial));
129+
device.TotalMemoryBytes ??= ParseTotalMemoryBytes(GetDeviceProperty(AdbProperty.MemInfo, device.DeviceSerial));
130+
device.CpuMaxFrequencyKiloHertz ??=
131+
ParseFrequencyKiloHertz(GetDeviceProperty(AdbProperty.CpuMaxFrequency, device.DeviceSerial, logOnError: false))
132+
?? ParseFrequencyKiloHertz(GetDeviceProperty(AdbProperty.CpuMaxFrequencyFallback, device.DeviceSerial, logOnError: false))
133+
?? ParseCpuFrequencyFromCpuInfoKiloHertz(GetDeviceProperty(AdbProperty.CpuInfo, device.DeviceSerial));
134+
}
135+
95136
private static string GetCliAdbExePath()
96137
{
97138
var currentAssemblyDirectory = Path.GetDirectoryName(typeof(AdbRunner).Assembly.Location);
@@ -1224,7 +1265,7 @@ private IReadOnlyCollection<AndroidDevice> GetDevices(params AdbProperty[] prope
12241265
return devices;
12251266
}
12261267

1227-
private string? GetDeviceProperty(AdbProperty property, string? deviceName = null)
1268+
private string? GetDeviceProperty(AdbProperty property, string? deviceName = null, bool logOnError = true)
12281269
{
12291270
IEnumerable<string> args = s_commandList[property];
12301271

@@ -1251,15 +1292,135 @@ private IReadOnlyCollection<AndroidDevice> GetDevices(params AdbProperty[] prope
12511292

12521293
if (!result.Succeeded)
12531294
{
1254-
_log.LogError($"Failed to get device's property {property}. Check if a device is attached / emulator is started" +
1255-
Environment.NewLine + result.StandardError);
1295+
if (logOnError)
1296+
{
1297+
_log.LogError($"Failed to get device's property {property}. Check if a device is attached / emulator is started" +
1298+
Environment.NewLine + result.StandardError);
1299+
}
12561300

12571301
return null;
12581302
}
12591303

12601304
return result.StandardOutput.Trim();
12611305
}
12621306

1307+
internal static string? ParseCpuModel(string? cpuInfo)
1308+
{
1309+
if (string.IsNullOrWhiteSpace(cpuInfo))
1310+
{
1311+
return null;
1312+
}
1313+
1314+
foreach (var line in cpuInfo.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
1315+
{
1316+
if (TryParseColonValue(line, "Hardware", out var hardware))
1317+
{
1318+
return hardware;
1319+
}
1320+
1321+
if (TryParseColonValue(line, "model name", out var modelName))
1322+
{
1323+
return modelName;
1324+
}
1325+
1326+
if (TryParseColonValue(line, "Processor", out var processor))
1327+
{
1328+
return processor;
1329+
}
1330+
}
1331+
1332+
return null;
1333+
}
1334+
1335+
internal static long? ParseCpuFrequencyFromCpuInfoKiloHertz(string? cpuInfo)
1336+
{
1337+
if (string.IsNullOrWhiteSpace(cpuInfo))
1338+
{
1339+
return null;
1340+
}
1341+
1342+
foreach (var line in cpuInfo.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
1343+
{
1344+
if (TryParseColonValue(line, "cpu MHz", out var cpuFrequency))
1345+
{
1346+
return double.TryParse(cpuFrequency, NumberStyles.Float, CultureInfo.InvariantCulture, out var megaHertz)
1347+
? (long)(megaHertz * 1000)
1348+
: null;
1349+
}
1350+
}
1351+
1352+
return null;
1353+
}
1354+
1355+
internal static long? ParseFrequencyKiloHertz(string? value)
1356+
{
1357+
if (string.IsNullOrWhiteSpace(value))
1358+
{
1359+
return null;
1360+
}
1361+
1362+
var trimmed = value.Trim();
1363+
if (long.TryParse(trimmed.Split(' ', StringSplitOptions.RemoveEmptyEntries)[0], out var kiloHertz))
1364+
{
1365+
return kiloHertz;
1366+
}
1367+
1368+
if (trimmed.EndsWith("MHz", StringComparison.OrdinalIgnoreCase)
1369+
&& double.TryParse(trimmed.Replace("MHz", "", StringComparison.OrdinalIgnoreCase).Trim(), out var megaHertz))
1370+
{
1371+
return (long)(megaHertz * 1000);
1372+
}
1373+
1374+
if (trimmed.EndsWith("GHz", StringComparison.OrdinalIgnoreCase)
1375+
&& double.TryParse(trimmed.Replace("GHz", "", StringComparison.OrdinalIgnoreCase).Trim(), out var gigaHertz))
1376+
{
1377+
return (long)(gigaHertz * 1_000_000);
1378+
}
1379+
1380+
return null;
1381+
}
1382+
1383+
internal static long? ParseTotalMemoryBytes(string? memInfo)
1384+
{
1385+
if (string.IsNullOrWhiteSpace(memInfo))
1386+
{
1387+
return null;
1388+
}
1389+
1390+
foreach (var line in memInfo.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
1391+
{
1392+
if (!TryParseColonValue(line, "MemTotal", out var totalMemory))
1393+
{
1394+
continue;
1395+
}
1396+
1397+
if (long.TryParse(totalMemory.Split(' ', StringSplitOptions.RemoveEmptyEntries)[0], out var kiloBytes))
1398+
{
1399+
return kiloBytes * 1024;
1400+
}
1401+
}
1402+
1403+
return null;
1404+
}
1405+
1406+
private static bool TryParseColonValue(string line, string key, out string value)
1407+
{
1408+
value = string.Empty;
1409+
if (!line.StartsWith(key, StringComparison.OrdinalIgnoreCase))
1410+
{
1411+
return false;
1412+
}
1413+
1414+
var separatorIndex = line.IndexOf(':');
1415+
if (separatorIndex < 0 || separatorIndex == line.Length - 1)
1416+
{
1417+
return false;
1418+
}
1419+
1420+
value = line.Substring(separatorIndex + 1).Trim();
1421+
return true;
1422+
}
1423+
12631424
private bool TestFileExists(string path, string? deviceName = null)
12641425
{
12651426
var deviceTestPath = GlobalReadWriteDirectory + Path.AltDirectorySeparatorChar + new DirectoryInfo(path).Name;

‎src/Microsoft.DotNet.XHarness.Android/AndroidDevice.cs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Generic;
67

78
namespace Microsoft.DotNet.XHarness.Android;
@@ -18,5 +19,23 @@ public record AndroidDevice
1819

1920
public IReadOnlyCollection<string>? InstalledApplications { get; set; } = null;
2021

22+
public string? Manufacturer { get; set; } = null;
23+
24+
public string? Model { get; set; } = null;
25+
26+
public string? ProductName { get; set; } = null;
27+
28+
public string? OperatingSystemVersion { get; set; } = null;
29+
30+
public string? BuildFingerprint { get; set; } = null;
31+
32+
public string? CpuModel { get; set; } = null;
33+
34+
public long? CpuMaxFrequencyKiloHertz { get; set; } = null;
35+
36+
public long? TotalMemoryBytes { get; set; } = null;
37+
38+
public bool IsEmulator => DeviceSerial.StartsWith("emulator", StringComparison.OrdinalIgnoreCase);
39+
2140
public AndroidDevice(string deviceSerial) => DeviceSerial = deviceSerial;
2241
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Linq;
7+
using Microsoft.DotNet.XHarness.Common;
8+
9+
namespace Microsoft.DotNet.XHarness.Android;
10+
11+
public static class AndroidEnvironmentReport
12+
{
13+
public static ExecutionEnvironmentInfo CreateEnvironmentInfo(AdbRunner runner, AndroidDevice device)
14+
{
15+
ArgumentNullException.ThrowIfNull(runner);
16+
ArgumentNullException.ThrowIfNull(device);
17+
18+
runner.PopulateEnvironmentInfo(device);
19+
20+
return new ExecutionEnvironmentInfo
21+
{
22+
Host = EnvironmentReportLogger.GetHostEnvironmentInfo(),
23+
Target = new TargetEnvironmentInfo
24+
{
25+
Kind = device.IsEmulator ? "emulator" : "physical-device",
26+
Name = device.Model ?? device.DeviceSerial,
27+
Identifier = device.DeviceSerial,
28+
OperatingSystem = "Android",
29+
OperatingSystemVersion = device.OperatingSystemVersion,
30+
ApiLevel = device.ApiVersion,
31+
Architecture = device.Architecture,
32+
SupportedArchitectures = device.SupportedArchitectures?.ToArray(),
33+
Manufacturer = device.Manufacturer,
34+
Model = device.Model,
35+
ProductName = device.ProductName,
36+
BuildFingerprint = device.BuildFingerprint,
37+
CpuModel = device.CpuModel,
38+
CpuMaxFrequencyHertz = device.CpuMaxFrequencyKiloHertz * 1000,
39+
TotalMemoryBytes = device.TotalMemoryBytes,
40+
},
41+
};
42+
}
43+
}

‎src/Microsoft.DotNet.XHarness.Android/InstrumentationRunner.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ private void EmitRunSummary(ExitCode exitCode, int? instrumentationExitCode, Lis
282282
{
283283
var device = _runner.GetActiveDevice();
284284
string? deviceOsVersion = device?.ApiVersion.HasValue == true ? $"API {device.ApiVersion}" : null;
285+
var environment = device == null ? null : AndroidEnvironmentReport.CreateEnvironmentInfo(_runner, device);
285286

286287
RunSummaryEmitter.EmitRunSummary(
287288
_logger,
@@ -291,7 +292,8 @@ private void EmitRunSummary(ExitCode exitCode, int? instrumentationExitCode, Lis
291292
deviceOsVersion: deviceOsVersion,
292293
architecture: device?.Architecture,
293294
instrumentationExitCode: instrumentationExitCode,
294-
producedFiles: producedFiles);
295+
producedFiles: producedFiles,
296+
environment: environment);
295297

296298
RunSummaryEmitter.WriteResultJsonFile(
297299
outputDirectory,
@@ -301,6 +303,7 @@ private void EmitRunSummary(ExitCode exitCode, int? instrumentationExitCode, Lis
301303
deviceOsVersion: deviceOsVersion,
302304
architecture: device?.Architecture,
303305
instrumentationExitCode: instrumentationExitCode,
304-
producedFiles: producedFiles);
306+
producedFiles: producedFiles,
307+
environment: environment);
305308
}
306309
}

0 commit comments

Comments
 (0)