C# - 无法将类型 'string[][]' 隐式转换为 'int[][]'

C# - cannot implicitly convert type 'string[][]' to 'int[][]'

我正在尝试用 C# 编写脚本来计算服务器的 CPU 使用率。为此,我正在使用 'cat /proc/stat',然后遵循本指南:how to calculate CPU usage

首先我需要找出 cpu_total_timecpu_idle_time 然后计算

cpu_usage_time = (cpu_total_time - cpu_idle_time) / cpu_total_time * delta 

其中 delta 是 cpu_oldcpu_new 中包含的读数之间的差异,以秒为单位。

到目前为止我的脚本是:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Threading.Tasks;

class Program
{
    public static void Main()
    {
        float delta = 300;
        string cpu_old = @"cpu  434473062 3549 100174857 65243175066 2350230 0 23538998 0 0 0
cpu0 40363141 158 9961795 4046071159 14998 0 2151386 0 0 0
cpu1 43114169 130 9906697 4044059231 14821 0 2322581 0 0 0
cpu2 46217091 118 10451188 4043699342 14271 0 2530557 0 0 0
cpu3 46520571 95 10394572 4046346748 12427 0 2533812 0 0 0
cpu4 44400015 73 9822374 4051834091 8572 0 2383614 0 0 0
cpu5 41373066 95 9107815 4057879672 9063 0 2207833 0 0 0
cpu6 40070307 94 8892688 4060344982 15899 0 2174872 0 0 0
cpu7 39194082 109 8715754 4062643570 14710 0 2110436 0 0 0
cpu8 14996205 327 3564661 4099822309 14742 0 840150 0 0 0
cpu9 12736725 346 3132467 4102694576 12765 0 715207 0 0 0
cpu10 10787760 349 2569831 4105053050 11834 0 587924 0 0 0
cpu11 10278285 269 2568664 4105585391 5650 0 550660 0 0 0
cpu12 9466026 331 2448787 4104518475 2159416 0 514311 0 0 0
cpu13 9650748 328 2437976 4106764105 10611 0 516373 0 0 0
cpu14 10903552 332 2733229 4105176301 12290 0 600582 0 0 0
cpu15 14401310 386 3466350 4100682055 18153 0 798693 0 0 0";
        // Counting the number of lines the cpu_old variable has (number of CPU cores)
        int numLines = cpu_old.Split('\n').Length;
        // Creating a 2-D array from the input string
        var cpu_usage_old =  cpu_old.Split('\n').Select(x => x.Replace("cpu ","").Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)).ToArray();
        string cpu_new = @"cpu  434474592 3549 100175256 65244055979 2350259 0 23539065 0 0 0
cpu0 40363255 158 9961820 4046126195 14998 0 2151391 0 0 0
cpu1 43114268 130 9906722 4044114283 14821 0 2322584 0 0 0
cpu2 46217222 118 10451220 4043754354 14271 0 2530564 0 0 0
cpu3 46520645 95 10394592 4046401839 12427 0 2533813 0 0 0
cpu4 44400082 73 9822391 4051889189 8572 0 2383616 0 0 0
cpu5 41373168 95 9107845 4057934721 9063 0 2207838 0 0 0
cpu6 40070438 94 8892720 4060399997 15899 0 2174878 0 0 0
cpu7 39194253 109 8715790 4062698541 14710 0 2110444 0 0 0
cpu8 14996240 327 3564671 4099877456 14742 0 840151 0 0 0
cpu9 12737022 346 3132532 4102749364 12765 0 715223 0 0 0
cpu10 10787804 349 2569845 4105108184 11834 0 587924 0 0 0
cpu11 10278327 269 2568684 4105640505 5650 0 550661 0 0 0
cpu12 9466052 331 2448797 4104573595 2159444 0 514312 0 0 0
cpu13 9650910 328 2438016 4106819087 10611 0 516382 0 0 0
cpu14 10903566 332 2733236 4105231472 12290 0 600582 0 0 0
cpu15 14401335 386 3466367 4100737188 18153 0 798694 0 0 0";
        var cpu_usage_new = cpu_new.Split('\n').Select(x => x.Replace("cpu ","").Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)).ToArray();
        var cpuUsage = new List<int>();
        foreach (var idx in Enumerable.Range(0, numLines))
        {       
              int cpu_time_old = cpu_usage_old[idx, 0] + cpu_usage_old[idx][1] + cpu_usage_old[idx][2] + cpu_usage_old[idx][3] + cpu_usage_old[idx][4] + cpu_usage_old[idx][5] + cpu_usage_old[idx][6] + cpu_usage_old[idx][7];
        }
    }
}

如您所见,我刚刚尝试在这里计算 cpu_usage_old,但其余的或多或少会很简单。 我看到我的问题有两种可能的解决方案:

  1. 在创建时将二维数组 cpu_usage_newcpu_usage_old 的所有元素转换为整数,然后使用矩阵的列,而不是 运行 a for环形。然后将该值添加到同一二维数组的新列中。
  2. 在 for 循环中转换 summing/subtracting 时的元素,但这将是一个很长且不太优雅的处理问题的方法。然后我可以将所有结果附加到 cpuUsage 列表中,其中每个值将是此特定 CPU 核心在指定的 delta 时间段内的 CPU 使用情况。

这是您发布的代码的更正版本。希望它能让您完成任务。

using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Threading.Tasks;

public class Program
{
    public static void Main()
    {
        float delta = 300;
        string cpu_old = @"cpu  434473062 3549 100174857 65243175066 2350230 0 23538998 0 0 0
cpu0 40363141 158 9961795 4046071159 14998 0 2151386 0 0 0
cpu1 43114169 130 9906697 4044059231 14821 0 2322581 0 0 0
cpu2 46217091 118 10451188 4043699342 14271 0 2530557 0 0 0
cpu3 46520571 95 10394572 4046346748 12427 0 2533812 0 0 0
cpu4 44400015 73 9822374 4051834091 8572 0 2383614 0 0 0
cpu5 41373066 95 9107815 4057879672 9063 0 2207833 0 0 0
cpu6 40070307 94 8892688 4060344982 15899 0 2174872 0 0 0
cpu7 39194082 109 8715754 4062643570 14710 0 2110436 0 0 0
cpu8 14996205 327 3564661 4099822309 14742 0 840150 0 0 0
cpu9 12736725 346 3132467 4102694576 12765 0 715207 0 0 0
cpu10 10787760 349 2569831 4105053050 11834 0 587924 0 0 0
cpu11 10278285 269 2568664 4105585391 5650 0 550660 0 0 0
cpu12 9466026 331 2448787 4104518475 2159416 0 514311 0 0 0
cpu13 9650748 328 2437976 4106764105 10611 0 516373 0 0 0
cpu14 10903552 332 2733229 4105176301 12290 0 600582 0 0 0
cpu15 14401310 386 3466350 4100682055 18153 0 798693 0 0 0";
        // Counting the number of lines the cpu_old variable has (number of CPU cores)
        int numLines = cpu_old.Split('\n').Length;

        // Creating a 2-D array from the input string
        var cpu_usage_old =  cpu_old.Split('\n').Select(x => x.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)).ToArray();

        string cpu_new = @"cpu  434474592 3549 100175256 65244055979 2350259 0 23539065 0 0 0
cpu0 40363255 158 9961820 4046126195 14998 0 2151391 0 0 0
cpu1 43114268 130 9906722 4044114283 14821 0 2322584 0 0 0
cpu2 46217222 118 10451220 4043754354 14271 0 2530564 0 0 0
cpu3 46520645 95 10394592 4046401839 12427 0 2533813 0 0 0
cpu4 44400082 73 9822391 4051889189 8572 0 2383616 0 0 0
cpu5 41373168 95 9107845 4057934721 9063 0 2207838 0 0 0
cpu6 40070438 94 8892720 4060399997 15899 0 2174878 0 0 0
cpu7 39194253 109 8715790 4062698541 14710 0 2110444 0 0 0
cpu8 14996240 327 3564671 4099877456 14742 0 840151 0 0 0
cpu9 12737022 346 3132532 4102749364 12765 0 715223 0 0 0
cpu10 10787804 349 2569845 4105108184 11834 0 587924 0 0 0
cpu11 10278327 269 2568684 4105640505 5650 0 550661 0 0 0
cpu12 9466052 331 2448797 4104573595 2159444 0 514312 0 0 0
cpu13 9650910 328 2438016 4106819087 10611 0 516382 0 0 0
cpu14 10903566 332 2733236 4105231472 12290 0 600582 0 0 0
cpu15 14401335 386 3466367 4100737188 18153 0 798694 0 0 0";
        var cpu_usage_new = cpu_new.Split('\n').Select(x => x.Replace("cpu ","").Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)).ToArray();
        var cpuUsage = new List<int>();
        foreach (var idx in Enumerable.Range(0, numLines))
        {       
              long cpu_time_old = Convert.ToInt64(cpu_usage_old[idx][1],10) + Convert.ToInt64(cpu_usage_old[idx][2],10) + Convert.ToInt64(cpu_usage_old[idx][3],10) + Convert.ToInt64(cpu_usage_old[idx][4],10) + Convert.ToInt64(cpu_usage_old[idx][5],10) + Convert.ToInt64(cpu_usage_old[idx][6],10) + Convert.ToInt64(cpu_usage_old[idx][7],10) + Convert.ToInt64(cpu_usage_old[idx][8],10);
        }
    }
}