Autore Topic: Libcpuid14 Extern  (Letto 3524 volte)

Offline eloaders

  • Gamberetto
  • *
  • Post: 38
    • Mostra profilo
Libcpuid14 Extern
« il: 02 Novembre 2016, 18:51:04 »
Hello. I do not know Italian. Therefore, I try to write in English. My natural language is Polish.
So sorry for the mistakes that occur.

I have a problem with the library libcpuid. I do not have enough of an idea to do what I want. I therefore ask you to help solve the problem.

Example code:
Codice: [Seleziona]
' Gambas module file

Library "libcpuid:14"

Public Struct cpu_id_t
vendor_str[64] As String
brand_str[64] As String
vendor As String
flags[256] As Byte
family As Integer
model As Integer
stepping As Integer
ext_family As Integer
ext_model As Integer
num_cores As Integer
num_logical_cpus As Integer
total_logical_cpus As Integer
l1_data_cache As Integer
l1_instruction_cache As Integer
l2_cache As Integer
l3_cache As Integer
l4_cache As Integer
l1_assoc As Integer
l2_assoc As Integer
l3_assoc As Integer
l4_assoc As Integer
l1_cacheline As Integer
l2_cacheline As Integer
l3_cacheline As Integer
l4_cacheline As Integer
cpu_codename[64] As String
sse_size As Integer
detection_hints[128] As Byte
End Struct

Private cpu_id_t As New Cpu_id_t
'Private Extern cpu_id_t(info As Cpu_id_t) As Integer
Public Sub Main()
 Dim si As New Cpu_id_t
 Print si.l1_assoc
 
End

Doxygen doc: http://libcpuid.sourceforge.net/doxy/index.html
Sources: https://github.com/anrieff/libcpuid

i hope that your help will help me to continue to learn to use libraries in C.

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.683
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #1 il: 02 Novembre 2016, 19:04:24 »
The Extern keyword is used to declare an external Function, not a structure !
« Ultima modifica: 02 Novembre 2016, 19:07:46 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline eloaders

  • Gamberetto
  • *
  • Post: 38
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #2 il: 02 Novembre 2016, 19:16:23 »
That's why the code below work?

Codice: [Seleziona]
' Gambas module file

Library "libc:6"

Public Struct sysinfo
  uptime As Long
  loads[3] As Long
  totalram As Long
  freeram As Long
  sharedram As Long
  bufferram As Long
  totalswap As Long
  freeswap As Long
  procs As Short
  pad As Short
  totalhigh As Long
  freehigh As Long
  mem_unit As Integer
End Struct
 
' int sysinfo (struct sysinfo *__info)
' Returns information on overall system statistics.
Private Extern sysinfo(info As Sysinfo) As Integer
Public Logs As New Logger

Public Sub Main()
 
 Dim si As New Sysinfo
 Dim err As Integer
   
  err = sysinfo(si)
  If err < 0 Then
   Logs("Libc6 Sysinfo Error", Logger.Error)
  Else
   Finfosys.TextBox58.Text = "Uptime: " & Date(0, 0, 0, 0, 0, 0, CInt(si.uptime * 1000))
  Endif
End

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.683
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #3 il: 02 Novembre 2016, 19:16:43 »
I saw your Structure declaration: pay attention to values type !!!

For example:
char vendor_str[VENDOR_STR_MAX];  ---> in Gambas:  vendor[16]  As Byte (16, because VENDOR_STR_MAX = 16)

 cpu_vendor_t ..... why did you declare it as String ?   It's an enumeration, so it's an Integer.
« Ultima modifica: 02 Novembre 2016, 19:41:02 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.683
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #4 il: 02 Novembre 2016, 19:20:53 »
That's why the code below work?


Because sysinfo( ) is a Function, declared in  /usr/include/x86_64-linux-gnu/sys/sysinfo.h .
Oh yes..... its parameter is a Structure that has same name:
sysinfo( ) Function name;
sysinfo {...} Structure name.    ;D

http://man7.org/linux/man-pages/man2/sysinfo.2.html
« Ultima modifica: 02 Novembre 2016, 19:42:16 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline eloaders

  • Gamberetto
  • *
  • Post: 38
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #5 il: 02 Novembre 2016, 19:22:42 »
You can help me write it correctly?

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.683
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #6 il: 02 Novembre 2016, 19:23:35 »
You can help me write it correctly?
Yes, I can.

Wait a minute: i re-write it for you.
« Ultima modifica: 02 Novembre 2016, 19:24:51 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline eloaders

  • Gamberetto
  • *
  • Post: 38
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #7 il: 02 Novembre 2016, 19:26:04 »
Thank you very much.

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.683
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #8 il: 02 Novembre 2016, 19:33:45 »
Therefore, it should be:

Codice: [Seleziona]
Public Struct cpu_id_t
  vendor_str[16] As Byte
  brand_str[64] As Byte
  vendor As Integer
  flags[128] As Byte
  family As Integer
  model As Integer
  stepping As Integer
  ext_family As Integer
  ext_model As Integer
  num_cores As Integer
  num_logical_cpus As Integer
  total_logical_cpus As Integer
  l1_data_cache As Integer
  l1_instruction_cache As Integer
  l2_cache As Integer
  l3_cache As Integer
  l4_cache As Integer
  l1_assoc As Integer
  l2_assoc As Integer
  l3_assoc As Integer
  l4_assoc As Integer
  l1_cacheline As Integer
  l2_cacheline As Integer
  l3_cacheline As Integer
  l4_cacheline As Integer
  cpu_codename[64] As Byte
  sse_size As Integer
  detection_hints[16] As Byte
End Struct

In my experience, however, you might have trouble by reading from those Byte[nn] type array members.
« Ultima modifica: 04 Novembre 2016, 13:18:35 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline eloaders

  • Gamberetto
  • *
  • Post: 38
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #9 il: 02 Novembre 2016, 19:39:02 »
Each value returns me 0. What could be the problem?

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.683
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #10 il: 02 Novembre 2016, 19:43:16 »
What's your Gambas code about ?

(please, show essencial part of that)
« Ultima modifica: 02 Novembre 2016, 19:45:03 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline eloaders

  • Gamberetto
  • *
  • Post: 38
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #11 il: 02 Novembre 2016, 19:47:05 »
I am the author of I-Nex. A code is needed me to get rid of external programs. But I not quite know how to do it. I do not know how to use libcpuid to make it work. The module now looks like this:

Codice: [Seleziona]
' Gambas module file

Library "libcpuid:14"

Public Struct cpu_id_t
  vendor_str[16] As Byte
  brand_str[64] As Byte
  vendor As Integer
  flags[128] As Byte
  family As Integer
  model As Integer
  stepping As Integer
  ext_family As Integer
  ext_model As Integer
  num_cores As Integer
  num_logical_cpus As Integer
  total_logical_cpus As Integer
  l1_data_cache As Integer
  l1_instruction_cache As Integer
  l2_cache As Integer
  l3_cache As Integer
  l4_cache As Integer
  l1_assoc As Integer
  l2_assoc As Integer
  l3_assoc As Integer
  l4_assoc As Integer
  l1_cacheline As Integer
  l2_cacheline As Integer
  l3_cacheline As Integer
  l4_cacheline As Integer
  cpu_codename[64] As Byte
  sse_size As Integer
  detection_hints[16] As Byte
End Struct

Private cpu_id_t As New Cpu_id_t
'Private Extern cpu_id_t(info As Cpu_id_t) As Integer
Public Sub Main()
 Dim si As New Cpu_id_t
 Print si.l1_assoc
 Print si.sse_size
 Print si.vendor
End

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.683
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #12 il: 02 Novembre 2016, 19:52:32 »

'Private Extern cpu_id_t(info As Cpu_id_t) As Integer

What's the exact external Function that inizializes the Structure ?



« Ultima modifica: 02 Novembre 2016, 19:56:52 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline eloaders

  • Gamberetto
  • *
  • Post: 38
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #13 il: 02 Novembre 2016, 20:01:51 »
In C it looks like this:

Codice: [Seleziona]
#include <stdio.h>
#include <libcpuid.h>
int main(void)
{
if (!cpuid_present()) {                                                // check for CPUID presence
printf("Sorry, your CPU doesn't support CPUID!\n");
return -1;
}
struct cpu_raw_data_t raw;
struct cpu_id_t data;                                                  // contains recognized CPU features data
if (cpuid_get_raw_data(&raw) < 0) {                                    // obtain the raw CPUID data
printf("Sorry, cannot get the CPUID raw data.\n");
printf("Error: %s\n", cpuid_error());                          // cpuid_error() gives the last error description
return -2;
}
if (cpu_identify(&raw, &data) < 0) {                                   // identify the CPU, using the given raw data.
printf("Sorrry, CPU identification failed.\n");
printf("Error: %s\n", cpuid_error());
return -3;
}
struct cpu_mark_t mark;
cpu_tsc_mark(&mark);
cpu_tsc_unmark(&mark);
printf("{\n");
printf(" \"VENDOR_STR\": \"%s\",\n", data.vendor_str);               //
printf(" \"CPU_CODENAME\": \"%s\",\n", data.cpu_codename);       //
printf(" \"BRAND_STR\": \"%s\",\n", data.brand_str);       //
printf(" \"NUM_CORES\": \"%d\",\n", data.num_cores);       //
printf(" \"NUM_LOGICAL_CPUS\": \"%d\",\n", data.num_logical_cpus);     //
printf(" \"TOTAL_LOGICAL_CPUS\": \"%d\",\n", data.total_logical_cpus); //
printf(" \"FAMILY\": \"%d\",\n", data.family);                         //
printf(" \"MODEL\": \"%d\",\n", data.model);                           //
printf(" \"STEPPING\": \"%d\",\n", data.stepping);                     //
printf(" \"EXT_FAMILY\": \"%d\",\n", data.ext_family);                 //
printf(" \"EXT_MODEL\": \"%d\",\n", data.ext_model);                   //
printf(" \"CPU_CLOCK\": \"%d MHz\",\n", cpu_clock());                      //
printf(" \"CPU_CLOCK_BY_OS\": \"%d MHz\",\n", cpu_clock_by_os());          //
printf(" \"CPU_CLOCK_BY_IC\": \"%d MHz\",\n", cpu_clock_by_ic(25, 16));    //
printf(" \"CPU_CLOCK_MEASURE\": \"%d MHz\",\n", cpu_clock_measure(400, 1));//
printf(" \"MARK_TSC\": \"%llu\",\n", mark.tsc);       //
printf(" \"MARK_SYS_CLOCK\": \"%llu\",\n", mark.sys_clock);            //

It seems that in order to initiate the data is sufficient to initiate:  cpu_identify() and cpuid_get_raw_data()
How to move to the code gambas?
« Ultima modifica: 02 Novembre 2016, 20:39:43 da eloaders »

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.683
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Libcpuid14 Extern
« Risposta #14 il: 03 Novembre 2016, 01:31:30 »
In C it looks like this:
.......
How to move to the code gambas?

Bah... I suggest something like this:

Codice: [Seleziona]
Library "libcpuid:14"

Public Struct cpu_raw_data_t      ' but we could replace this Structure by an allocated memory area  by using Alloc( ) function
  basic_cpuid[32, 4] As Integer
  ext_cpuid[32, 4] As Integer
  intel_fn4[4, 4] As Integer
  intel_fn11[4, 4] As Integer
End Struct

Public Struct cpu_id_t
  vendor_str[16] As Byte
  brand_str[64] As Byte
  vendor As Integer
  flags[128] As Byte
  family As Integer
  model As Integer
  stepping As Integer
  ext_family As Integer
  ext_model As Integer
  num_cores As Integer
  num_logical_cpus As Integer
  total_logical_cpus As Integer
  l1_data_cache As Integer
  l1_instruction_cache As Integer
  l2_cache As Integer
  l3_cache As Integer
  l4_cache As Integer
  l1_assoc As Integer
  l2_assoc As Integer
  l3_assoc As Integer
  l4_assoc As Integer
  l1_cacheline As Integer
  l2_cacheline As Integer
  l3_cacheline As Integer
  l4_cacheline As Integer
  cpu_codename[64] As Byte
  sse_size As Integer
  detection_hints[16] As Byte
End Struct

Public Struct cpu_mark_t
  tsc As Long
  sys_clock As Long
End Struct

' int cpuid_present(void)
Private Extern cpuid_present() As Integer

' int cpuid_get_raw_data(struct cpu_raw_data_t* data)
Private Extern cpuid_get_raw_data(data As Cpu_raw_data_t) As Integer

' const char* cpuid_error(void)
Private Extern cpuid_error() As String

' int cpu_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data)
Private Extern cpu_identify(raw As Cpu_raw_data_t, data As Cpu_id_t) As Integer

' void cpu_tsc_mark(struct cpu_mark_t* mark)
Private Extern cpu_tsc_mark(mark As Cpu_mark_t)

' void cpu_tsc_unmark(struct cpu_mark_t* mark)
Private Extern cpu_tsc_unmark(mark As Cpu_mark_t)

' int cpu_clock(void)
Private Extern cpu_clock() As Integer

' int cpu_clock_by_os(void)
Private Extern cpu_clock_by_os() As Integer

' int cpu_clock_by_ic(int millis, int runs)
Private Extern cpu_clock_by_ic(millis As Integer, runs As Integer) As Integer

' int cpu_clock_measure(int millis, int quad_check)
Private Extern cpu_clock_measure(millis As Integer, quad_check As Integer) As Integer


Public Sub Main()

  Dim raw As New Cpu_raw_data_t
  Dim data As New Cpu_id_t
  Dim mark As New Cpu_mark_t
 
 
    If Not cpuid_present() Then Error.Raise("Sorry, your CPU doesn't support CPUID !")
   
    If cpuid_get_raw_data(raw) < 0 Then
      Error.Raise("Sorry, cannot get the CPUID raw data.\nError: " & cpuid_error())
    Endif
   
    If cpu_identify(raw, data) < 0 Then
      Error.Raise("Sorry, CPU identification failed.\nError: " & cpuid_error())
    Endif
   
    cpu_tsc_mark(mark)
    cpu_tsc_unmark(mark)
   
    Print "   \"VENDOR_STR\":         "; data.vendor_str
    Print "   \"CPU_CODENAME\":       "; data.cpu_codename
    Print "   \"BRAND_STR\":          "; data.brand_str
    Print "   \"NUM_CORES\":          "; data.num_cores
    Print "   \"NUM_LOGICAL_CPUS\":   "; data.num_logical_cpus
    Print "   \"TOTAL_LOGICAL_CPUS\": "; data.total_logical_cpus
    Print "   \"FAMILY\":             "; data.family
    Print "   \"MODEL\":              "; data.model
    Print "   \"STEPPING\":           "; data.stepping
    Print "   \"EXT_FAMILY\":         "; data.ext_family
    Print "   \"EXT_MODEL\":          "; data.ext_model
    Print "   \"CPU_CLOCK\":          "; cpu_clock()
    Print "   \"CPU_CLOCK_BY_OS\":    "; cpu_clock_by_os(); " Mhz"
    Print "   \"CPU_CLOCK_BY_IC\":    "; cpu_clock_by_ic(25, 16); " Mhz"
    Print "   \"CPU_CLOCK_MEASURE\":  "; cpu_clock_measure(400, 1); " Mhz"
    Print "   \"MARK_TSC\":           "; mark.tsc
    Print "   \"MARK_SYS_CLOCK\":     "; mark.sys_clock

End
« Ultima modifica: 03 Novembre 2016, 16:07:16 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »