Windows stability

General discussion for topics related to the FreeBASIC project or its community.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Windows stability

Post by srvaldez »

am I the only person that finds Windows OS unstable?
after 4 months Windows CPU usage while idle would range between 8% to 15% seldom falling below that for maybe a second or so
I restored the OS from a 4 month old backup and after spending 4 hours updating and then making a new backup it seems stable at least for now
the CPU usage as I am typing this is between 0.8% and 5%
St_W
Posts: 1618
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Windows stability

Post by St_W »

Not sure what CPU usage during idle time has to do with system stability?
Anyways, probably just some services you either installed (like Chrome Updater) or included with Windows (like Microsoft Compatibility Telemetry). If you don't see any userspace process having the CPU load, but rather some system process, then it's likely to be caused by some crappy driver. What system do you use? If it's e.g. some cheap chinese hardware the firmware and drivers are likely to be equally cheap and bad.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Windows stability

Post by srvaldez »

I don't think that it's Chinese but you never know until you inspect the components, right now after running the msys2 shell the CPU usage is about 7% on idle, the shell being terminated, so I don't know what to make of this
Provoni
Posts: 513
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Windows stability

Post by Provoni »

Sounds like a quad core CPU with hyperthreading.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Windows stability

Post by srvaldez »

I post the following here because it may be related to a windows quirk.
this used to run ok but now I get
The system cannot execute the specified program.


------------------
(program exited with code: 9020)

Code: Select all

Function rdrand() As Integer
	Dim As Integer n
	Asm
		mov eax, 7
		Xor ecx, ecx
		cpuid
		bt ebx, 18
		jnc 0
		rdseed rax
		mov qword Ptr [n], rax
		0:
	End Asm
	Return n
End Function

Dim As Integer i, n

For i=1 To 100	
	n=rdrand()
	Print i, n
Next

Sleep
and I get the error regardless whether I call the function rdrand() or not, the following works on my PC

Code: Select all

Dim As Integer i, n

For i=1 To 100	
	asm
		rdseed rax
		mov [n], rax
	end asm
	Print i, n
Next

Sleep
warning, if your CPU doesn't support the rdseed instruction then this program will likely crash
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Windows stability

Post by srvaldez »

the reason I restored my system was because I was very suspicious about the CPU activity, I suspected that some malicious code was running -- perhaps some ransomware program busy encrypting
[edit]
using FB 1.09.0 gcc-9.3.0 my program works
this could be a binutils issue because before I was using the latest
I would like to know if anyone running FB with the latest binuils has the problem I was having
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Windows stability

Post by srvaldez »

the culprit is ld version 2.38, ld version 2.34 works ok
now to check on Linux
[edit]
no problem on Linux using binutils-2.38
this issue could be related to viewtopic.php?p=287143#p287143
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Windows stability

Post by dodicat »

For fun, Windows:

Code: Select all

#include "windows.bi"

Function getSystemCPUTime(Byref totalTime  As Ulongint, Byref idleTime As Ulongint)As boolean
      Dim As FILETIME ftSysIdle, ftSysKernel, ftSysUser
      If (GetSystemTimes(@ftSysIdle, @ftSysKernel, @ftSysUser))=0 Then Return false
      Dim As  ULARGE_INTEGER sysKernel, sysUser, sysIdle
      sysKernel.HighPart = ftSysKernel.dwHighDateTime
      sysKernel.LowPart = ftSysKernel.dwLowDateTime
      sysUser.HighPart = ftSysUser.dwHighDateTime
      sysUser.LowPart = ftSysUser.dwLowDateTime
      sysIdle.HighPart = ftSysIdle.dwHighDateTime
      sysIdle.LowPart = ftSysIdle.dwLowDateTime
      totalTime = sysKernel.QuadPart + sysUser.QuadPart
      idleTime = sysIdle.QuadPart
      Return true
End Function

Function map(a As Double,b As Double,x As Double,c As Double,d As Double) As Double
      Return (d-c)*(x-a)/(b-a)+c
End Function

Sub Line_To(x1 As Long,y1 As Long,x2 As Long,y2 As Long,d As Single,Byref x As Long=0,Byref y As Long=0,i As Any Ptr=0) 
      x=x1+(x2-x1)*d
      y=y1+(y2-y1)*d
End Sub

Sub thick_line(x1 As Long,y1 As Long,x2 As Long,y2 As Long,thickness As Single, colour As Ulong,im As Any Ptr=0)
      If thickness<2 Then
            Line(x1,y1)-(x2,y2),colour
      Else               
            Var h=Sqr((x2-x1)^2+(y2-y1)^2) 
            If h=0 Then h=1e-6
            Var s=(y1-y2)/h               
            Var c=(x2-x1)/h                 
            Line im,(x1+s*thickness/2,y1+c*thickness/2)-(x2+s*thickness/2,y2+c*thickness/2),colour
            Line im,(x1-s*thickness/2,y1-c*thickness/2)-(x2-s*thickness/2,y2-c*thickness/2),colour
            Line im,(x1+s*thickness/2,y1+c*thickness/2)-(x1-s*thickness/2,y1-c*thickness/2),colour
            Line im,(x2+s*thickness/2,y2+c*thickness/2)-(x2-s*thickness/2,y2-c*thickness/2),colour
            Paint im,((x1+x2)/2, (y1+y2)/2), colour, colour
      End If
End Sub

Sub drawline(x As Long,y As Long,angle As Single,lngth As Double,Byref x2 As Long=0,Byref y2 As Long=0)
      angle=angle*Atn(1)/45
      x2=x+lngth*Cos(angle)
      y2=y-lngth*Sin(angle)
End Sub

Sub digits(t As String,x As long,y As long,clr As Ulong,sz As single,gap as long=1,img As Any Pointer=0)
        x=x-2*sz
        dim as single s=any,c=any
        dim as single  d =iif(gap, sz/10,0)
        #macro thickline(x1,y1,x2,y2)
             s=(y1-y2)/10
             c=(x2-x1)/10
            Line img,(x1-s,y1-c)-(x2+s,y2+c),clr,bf
        #endmacro
        #macro display(_a,_b,_c,_d,_e,_f,_g)
        x=x+2*sz
        If _a=1 Then :thickline(x+d,y,(x-d+sz),y):End If 
        If _b=1 Then :thickline((x+sz),y+d,(x+sz),(y-d+sz)):End If
        If _c=1 Then :thickline((x+sz),(y+d+sz),(x+sz),(y-d+2*sz)):End If 
        If _d=1 Then :thickline((x-d+sz),(y+2*sz),x+d,(y+2*sz)):End If ''
        If _e=1 Then :thickline(x,(y-d+2*sz),x,(y+d+sz)):End If
        If _f=1 Then :thickline(x,(y-d+sz),x,y+d):End If
        If _g=1 Then :thickline(x+d,(y+sz),(x-d+sz),(y+sz)):End If 
        #endmacro
        For z As long=0 To Len(t)-1
            Select Case As Const t[z]
            Case 48 :display(1,1,1,1,1,1,0)             '"0"
            Case 49 :display(0,1,1,0,0,0,0)             '"1"
            Case 50 :display(1,1,0,1,1,0,1)             '"2"
            Case 51 :display(1,1,1,1,0,0,1)             '"3"
            Case 52 :display(0,1,1,0,0,1,1)             '"4"
            Case 53 :display(1,0,1,1,0,1,1)             '"5"
            Case 54 :display(1,0,1,1,1,1,1)             '"6"
            Case 55 :display(1,1,1,0,0,0,0)             '"7"
            Case 56 :display(1,1,1,1,1,1,1)             '"8"
            Case 57 :display(1,1,1,1,0,1,1)            '"9"
            Case 58                                     '":"                   
                Circle img,((x+2*sz),(y+sz/2)),(sz/5),clr,,,,f
                Circle img,((x+2*sz),(y+1.5*sz)),(sz/5),clr,,,,f
                x=x+sz 
            Case 45 :display(0,0,0,0,0,0,1)              '"-"                       
            Case 46                                      '"."                       
                Circle img,((x+2*sz),(y+1.9*sz)),(sz/5),clr,,,,f
                x=x+sz 
            Case 32                                      '" "
                x=x+sz 
            End Select
        Next z
    End Sub  
    
Sub createdial(i As Any Ptr)
      Circle i,(400,300),265,Rgb(0,100,255),,,,f
      Circle i,(273,443),2,0,,,,f
      Circle i,(524,439),2,0,,,,f
      dim as any ptr i2=imagecreate(30,30,Rgb(0,100,255))
      thick_line(5,25,25,5,3,rgb(200,200,200),i2) 
      circle i2,(15-4,5),3,rgb(200,200,200),,,,f
      circle i2,(15+4,25),3,rgb(200,200,200),,,,f
      put i,(435,345),i2,pset
      Dim As Long a,b,x,y
      Var z=6
      For n As Single=-40-z To 220+z Step 1.7
            Dim As Long k=map(-40-z,220+z,n,100,0)
            If k Mod 10=0 Then
                  drawline(400,300,n,220,a,b)
                  Line_To(a,b,400,300,.2,x,y)
                  Line i,(a,b)-(x,y),Rgb(0,0,0)
                  Line_To(a,b,400,300,-.1,x,y)
                  digits(str(int(k)),x-8-2,y-5,rgb(255,255,255),6,0,i)
            End If
            If k Mod 5=0 And n Mod 10<>0  Then
                  drawline(400,300,n,220,a,b)
                  Line_To(a,b,400,300,.05,x,y)
                  Line i,(a,b)-(x,y),Rgb(0,0,0)
            End If
      Next n
      imagedestroy i2
End Sub

Function odometer(v As Long) As Long
      Dim As Double k=map(100,0,v,-46,226)
      Dim As Long x,y,a,b
      drawline(400,300,k,200,x,y)
      Line_To(400,300,x,y,1,a,b)
      thick_line(400,300,a,b,8,Rgb(200,200,200)) 
      Line_To(400,300,x,y,-.1,a,b)
      thick_line(400,300,a,b,8,Rgb(200,200,200)) 
      Circle(400,300),2,Rgb(0,0,0),,,,f
      digits(Right("000"+Str((v))+"%",4),380,350,rgb(200,200,200),10)
      Return v
End Function

Function GetCPU(im As Any Ptr) As Long
      Dim As Ulong interval = 1000\2
      Dim As ULONGLONG totalPrev = 0, totalCurr = 0
      Dim As ULONGLONG idlePrev = 0, idleCurr = 0
      
      Dim As Ulong totalCPUUsage = 0
      Dim As Long result = getSystemCPUTime(totalPrev, idlePrev)
      While true
            Sleep(interval)
            If (getSystemCPUTime(totalCurr, idleCurr)) Then
                  Dim As  LONGLONG total = totalCurr - totalPrev
                  If (total > 0) Then
                        Dim As  LONGLONG idle = idleCurr - idlePrev
                        totalCPUUsage = (100 * (total - idle) / (total))
                        Screenlock
                        Put(0,0),im,Pset
                        odometer(totalCPUUsage)
                        Screenunlock
                        If Inkey=Chr(27) Then Return 0
                  End If
                  totalPrev = totalCurr
                  idlePrev = idleCurr
            End If
      Wend
      Return totalCPUUsage
End Function


Screen 19,32
windowtitle "CPU usage -- escape to finish"
Dim As Any Ptr im=Imagecreate(800,600,rgb(255,100,0))'Rgb(255,100,0))
createdial(im)
GetCPU(im)

Imagedestroy im

 
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Windows stability

Post by srvaldez »

nice dodicat :)
but I think that you should have 1 decimal point value so it would go from 0 to .1 .2 or 1.5 and so on
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Windows stability

Post by srvaldez »

solution to the problem

Code: Select all

function rdrand() as integer
	dim as integer n
	asm
		mov eax, 7
		xor ecx, ecx
		cpuid
		bt ebx, 18
		jnc 0f
		rdseed rax
		mov qword ptr [n], rax
		0:
	end asm
	return n
end function

dim as integer i, n

for i=1 to 100	
	n=rdrand()
	? i, n
next

sleep
can you spot the change from post above ? viewtopic.php?p=292253#p292253
one character is all it took
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Windows stability

Post by dodicat »

f

but I get zero for all the values of n
SARG
Posts: 1755
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Windows stability

Post by SARG »

Hi srvaldez

'f ' added.

Maybe the explanation:
(note : jnc is equivalent to jae)

The asm code with f
4015b5: 73 08 jae 0x4015bf <------ instruction relative 8 bytes
4015b7: 48 0f c7 f8 rdseed rax
4015bb: 48 89 45 80 mov QWORD PTR [rbp-0x80],rax
4015bf: 48 8b 45 80 mov rax,QWORD PTR [rbp-0x80]

without f
4015b1: 0f ba e3 12 bt ebx,0x12
4015b5: 0f 83 7c ea bf ff jae 0x37 <----- instruction relative 32bit (I don't understand the value) causing the issue with the 'new' linker.
4015bb: 48 0f c7 f8 rdseed rax
4015bf: 48 89 45 80 mov QWORD PTR [rbp-0x80],rax
4015c3: 48 8b 45 80 mov rax,QWORD PTR [rbp-0x80]

Hi dodicat,
I guess that your CPU cant' execute rdseed (tested with cpuid) so rax is set to null (current value of n), the returned value.



Edit:
Just tested using ld 2.38 from winlibs (GNU ld (Binutils for MinGW-W64 x86_64, built by Brecht Sanders) 2.38)
Without 'f' the created exe is not executable....

Using 2.34 no problem.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Windows stability

Post by srvaldez »

dodicat, I should have written a better example which would give a meaningful message when the instruction rdseed isn't avalable
thank you SARG for looking into it :)
one thing that disturbs me is that there are no warnings or errors when compiling, only to find out that the exe is not valid
it seems that with the new linker you must use relative addresses
SARG
Posts: 1755
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Windows stability

Post by SARG »

@srvaldez

0f 83 7c ea bf ff --> jae 0x37 is relative 32bit.
But as I wrote I don't understand the value 7c ea bf ff.

Trying to use objdump on the bad exe gave nothing : File format not recognized.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Windows stability

Post by srvaldez »

looks like a bug in the binutils 2.38
Post Reply