C # - não é possível converter implicitamente o tipo 'string [] []' em 'int [] []'

Georgi Stoyanov

Estou tentando escrever um script em c # para calcular o uso da CPU de um servidor. Para isso, estou usando 'cat / proc / stat' e, em seguida, estou seguindo este guia: como calcular o uso da CPU .

Primeiro preciso descobrir o cpu_total_timee o cpu_idle_timee, em seguida, calcular o

cpu_usage_time = (cpu_total_time - cpu_idle_time) / cpu_total_time * delta 

onde delta é a diferença entre as leituras contidas em cpu_olde cpu_newem segundos.

Meu script até agora é:

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];
        }
    }
}

Como você pode ver, tentei calcular o valor cpu_usage_oldaqui, mas o resto seria mais ou menos direto. Vejo duas soluções possíveis para o meu problema:

  1. Converter todos os elementos dos 2D matrizes cpu_usage_newe cpu_usage_oldem números inteiros no momento da criação e, em seguida, trabalhar com as colunas da matriz, em vez de executar um loop. Em seguida, adicione o valor a uma nova coluna da mesma matriz 2D.
  2. Converta os elementos no momento de somar / subtrair dentro do loop for, mas essa será uma maneira longa e não tão elegante de lidar com o problema. Posso então anexar todos os resultados à cpuUsagelista em que cada valor será o uso da CPU desse núcleo de CPU específico para o deltaperíodo de tempo especificado .
Javier Silva Ortíz

Aqui está a versão corrigida do código que você postou. Esperançosamente, isso permitirá que você conclua sua tarefa.

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);
        }
    }
}

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

Erro 2 Não é possível converter implicitamente o tipo 'int' em 'string' c #

Não é possível converter implicitamente o tipo em int []

C # Retorna o erro 'Não é possível converter implicitamente o tipo `int' em` string` ', embora nenhum int seja fornecido

Não é possível converter implicitamente o tipo 'int' em 'string' para uma matriz de string

C # - Não é possível converter implicitamente o tipo `int 'em` int [] []' - CodeFights

Mensagem de erro "Não é possível converter implicitamente o tipo 'String' em 'Int'"

Não é possível converter implicitamente o tipo 'string' em 'int' para comparar 2 variáveis

Não é possível converter implicitamente o tipo `string 'em` System.Collections.Generic.List <int>'

CS0029 C # 'Não é possível converter implicitamente o tipo string [] em string'

Não é possível converter implicitamente a string em int

C # - Tentando converter dois bytes em ushort resultando no erro "Não é possível converter implicitamente tipo 'int' em 'ushort'"

Não é possível converter implicitamente a string de tipo em System.Func <string, int, string, string>

Não é possível converter implicitamente o tipo 'IEnumerable <int> em' int '

Problema com matriz (não é possível converter implicitamente o tipo 'int' em 'int []')

C #: Erro - Não é possível converter implicitamente o tipo 'int' em 'System.Collections.Generic.IEnumerable <double>'

Não é possível converter implicitamente a string de tipo em string [] na chamada de sabão c #

Não é possível converter implicitamente o tipo *** em ****

Atualização condicional da Acumatica - Não é possível converter implicitamente o tipo 'string' em 'bool'

Erro: não é possível converter implicitamente a expressão ... do tipo int em ubyte

Não é possível converter implicitamente o tipo 'double' em 'int'

Não é possível converter implicitamente o tipo 'double' em 'int'

Não é possível converter implicitamente o tipo "int" em "bool"

Não é possível converter implicitamente o tipo 'System.Collections.Generic.IEnumerable <string> em string

Não é possível converter implicitamente o tipo 'System.Collections.Generic.List <string>' em 'string'

Como corrigir o erro 'Não é possível converter implicitamente o tipo' em C #?

Não é possível converter implicitamente int em int []

Erro Não é possível converter implicitamente o tipo 'string' em 'System.DateTime' no retorno

Não é possível converter implicitamente o tipo 'string' em 'decimal'

Não é possível converter implicitamente o tipo 'System.Linq.IQueryable <char []>' em 'string []'

TOP lista

quentelabel

Arquivo