Files
amiga/AmberMonitor/monitor-active-probe.c
T

79 lines
7.6 KiB
C

#include <exec/types.h>
#include <exec/execbase.h>
#include <exec/memory.h>
#include <exec/interrupts.h>
#include <hardware/intbits.h>
#include <intuition/intuition.h>
#include <graphics/gfxbase.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "colors.h"
#include "trig.h"
#define W 520
#define H 470
struct IntuitionBase *IntuitionBase;struct GfxBase *GfxBase;
extern struct ExecBase *SysBase;
static UBYTE *face,*frame,*out,*font;static LONG pens[64];
static volatile ULONG spin;
static int offset=0;
struct Sampling {struct ExecBase *base;struct Task *probe;volatile ULONG total,idle;};
static struct Sampling sample;
static void idleprobe(void){for(;;)spin++;}
static ULONG __saveds sampler(__reg("a1") struct Sampling *s){s->total++;if(s->base->ThisTask==s->probe)s->idle++;return 0;}
static void pixel(int x,int y,int c){if(x>=0&&x<W&&y>=0&&y<H)frame[y*W+x]=(UBYTE)pens[c];}
static void rect(int x,int y,int w,int h,int c){int a,b;for(b=0;b<h;b++)for(a=0;a<w;a++)pixel(x+a,y+b,c);}
static void line(int x0,int y0,int x1,int y1,int c){int dx=abs(x1-x0),sx=x0<x1?1:-1,dy=-abs(y1-y0),sy=y0<y1?1:-1,e=dx+dy,e2;for(;;){pixel(x0,y0,c);if(x0==x1&&y0==y1)break;e2=e*2;if(e2>=dy){e+=dy;x0+=sx;}if(e2<=dx){e+=dx;y0+=sy;}}}
static void text(int x,int y,char*s,int c,int scale){int a,b,k,z,ch;for(k=0;s[k];k++){ch=(unsigned char)s[k]-32;if(ch<0||ch>=96)continue;for(b=0;b<12;b++)for(a=0;a<8;a++)if(font[b*768+ch*8+a])for(z=0;z<scale;z++)rect(x+(k*8+a)*scale,y+b*scale+z,scale,1,c);}}
static int load(char*name,UBYTE*p,long n){FILE*f=fopen(name,"rb");int ok;if(!f)return 0;ok=fread(p,1,n,f)==n;fclose(f);return ok;}
struct Row {char name[37];int state,pri;};static struct Row rows[6];
static int tasks(struct Task*probe){struct Node*n;int count=0,r=0,k;struct Task*t;Forbid();
for(k=0;k<3;k++){
if(k==0)n=(struct Node*)SysBase->ThisTask;else n=(k==1?SysBase->TaskReady:SysBase->TaskWait).lh_Head;
while(n&&(k==0||n->ln_Succ)){
t=(struct Task*)n;if(t!=probe){count++;if(count>offset&&r<6){strncpy(rows[r].name,n->ln_Name?n->ln_Name:"(unnamed)",36);rows[r].name[36]=0;rows[r].state=t->tc_State;rows[r].pri=n->ln_Pri;r++;}}
if(k==0)break;n=n->ln_Succ;
}
}Permit();for(;r<6;r++)rows[r].name[0]=0;if(offset>=count)offset=0;return count;}
int main(void){struct Screen*sc=0;struct Window*w=0;struct IntuiMessage*m;struct Task*probe=0;UBYTE*stack=0;struct Interrupt irq;int irqon=0,taskon=0,got=0,done=0,i,x,y,iw,ih,rw,rh,ox,oy,loadpct=0,history[60],histn=0,nt=0,step=0;ULONG total,idle,cf,ct,ff,ft;char buf[100],cpu[24];int angle,a,xx,yy;ULONG cls;UWORD code;FILE*f;
SysBase=*(struct ExecBase**)4;
IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",39);GfxBase=(struct GfxBase*)OpenLibrary("graphics.library",40);if(!IntuitionBase||!GfxBase)goto end;
sc=LockPubScreen(0);if(!sc)goto end;face=malloc(W*H);frame=malloc(W*H);font=malloc(768*12);out=malloc((ULONG)sc->Width*sc->Height);if(!face||!frame||!font||!out)goto end;
if(!load("PROGDIR:face.bin",face,W*H)||!load("PROGDIR:font.bin",font,768*12))goto end;
for(i=0;i<64;i++){pens[i]=ObtainBestPen(sc->ViewPort.ColorMap,colors[i][0]*0x01010101UL,colors[i][1]*0x01010101UL,colors[i][2]*0x01010101UL,TAG_DONE);if(pens[i]<0)goto end;got++;}for(i=0;i<W*H;i++)face[i]=(UBYTE)pens[face[i]];
strcpy(cpu,(SysBase->AttnFlags&AFF_68060)?"MOTOROLA 68060":(SysBase->AttnFlags&AFF_68040)?"MOTOROLA 68040":(SysBase->AttnFlags&AFF_68030)?"MOTOROLA 68030":(SysBase->AttnFlags&AFF_68020)?"MOTOROLA 68020":"MOTOROLA 68000");
probe=AllocMem(sizeof(struct Task),MEMF_PUBLIC|MEMF_CLEAR);stack=AllocMem(8192,MEMF_PUBLIC|MEMF_CLEAR);if(!probe||!stack)goto end;
probe->tc_Node.ln_Type=NT_TASK;probe->tc_Node.ln_Pri=-127;probe->tc_Node.ln_Name="AmberMonitor.idle";
probe->tc_SPLower=stack;probe->tc_SPUpper=stack+8192;probe->tc_SPReg=stack+8192;
probe->tc_MemEntry.lh_Head=(struct Node*)&probe->tc_MemEntry.lh_Tail;probe->tc_MemEntry.lh_TailPred=(struct Node*)&probe->tc_MemEntry.lh_Head;
sample.base=SysBase;sample.probe=probe;
memset(&irq,0,sizeof(irq));irq.is_Node.ln_Type=NT_INTERRUPT;irq.is_Node.ln_Name="AmberMonitor.sample";irq.is_Node.ln_Pri=0;irq.is_Data=&sample;irq.is_Code=(VOID(*)())sampler;
AddIntServer(INTB_VERTB,&irq);irqon=1;AddTask(probe,(APTR)idleprobe,0);taskon=1;
memset(history,0,sizeof(history));
w=OpenWindowTags(0,WA_PubScreen,(ULONG)sc,WA_Left,352,WA_Top,180,WA_InnerWidth,W,WA_InnerHeight,H,WA_Title,(ULONG)"Amber Monitor | CPU virtuale",WA_CloseGadget,TRUE,WA_DragBar,TRUE,WA_DepthGadget,TRUE,WA_SizeGadget,TRUE,WA_SizeBBottom,TRUE,WA_MinWidth,360,WA_MinHeight,330,WA_MaxWidth,sc->Width,WA_MaxHeight,sc->Height,WA_SimpleRefresh,TRUE,WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_RAWKEY|IDCMP_REFRESHWINDOW,TAG_DONE);if(!w)goto end;
while(!done){while((m=(struct IntuiMessage*)GetMsg(w->UserPort))!=0){cls=m->Class;code=m->Code;ReplyMsg((struct Message*)m);if(cls==IDCMP_CLOSEWINDOW||(cls==IDCMP_RAWKEY&&code==0x45))done=1;if(cls==IDCMP_RAWKEY&&code==0x4d)offset+=6;if(cls==IDCMP_RAWKEY&&code==0x4c){offset-=6;if(offset<0)offset=0;}if(cls==IDCMP_REFRESHWINDOW){BeginRefresh(w);EndRefresh(w,TRUE);}}
Disable();total=sample.total;idle=sample.idle;if(total>=50){sample.total=sample.idle=0;}Enable();
if(total>=50){loadpct=(int)((total-idle)*100/total);if(loadpct>100)loadpct=100;memmove(history,history+1,59*sizeof(int));history[59]=loadpct;if(histn<60)histn++;}
cf=AvailMem(MEMF_CHIP);ct=AvailMem(MEMF_CHIP|MEMF_TOTAL);ff=AvailMem(MEMF_FAST);ft=AvailMem(MEMF_FAST|MEMF_TOTAL);nt=tasks(probe);
memcpy(frame,face,W*H);text(26,51,cpu,1,1);text(272,51,"CLOCK: DINAMICO / MAX + JIT",5,1);
for(i=0;i<40;i++)if(i*100/40<loadpct){angle=2250+i*2700/40;a=(angle%3600)*2;xx=109+trig[a]*70/10000;yy=153+trig[a+1]*70/10000;for(x=-1;x<=1;x++)line(xx+x,yy,109+trig[a]*79/10000+x,153+trig[a+1]*79/10000,loadpct>85?8:2);}
sprintf(buf,"%3d%%",loadpct);text(76,141,buf,2,2);text(54,181,"CPU / STIMA",5,1);
for(i=1;i<60;i++)if(i>=60-histn){int x0=232+(i-1)*260/59,x1=232+i*260/59;line(x0,201-history[i-1],x1,201-history[i],6);line(x0,202-history[i-1],x1,202-history[i],6);}
text(232,209,"CAMPIONAMENTO EXEC / 50 Hz",5,1);
rect(26,268,218,10,4);rect(270,268,218,10,7);if(ct)rect(26,268,(ct-cf)/(ct/218+1),10,2);if(ft)rect(270,268,(ft-ff)/(ft/218+1),10,6);
sprintf(buf,"%lu / %lu KB",(ct-cf)/1024,ct/1024);text(26,284,buf,1,1);sprintf(buf,"%lu / %lu KB",(ft-ff)/1024,ft/1024);text(270,284,buf,1,1);
sprintf(buf,"LIBERA %lu KB",cf/1024);text(26,302,buf,5,1);sprintf(buf,"LIBERA %lu KB",ff/1024);text(270,302,buf,5,1);
sprintf(buf,"%d TASK %d-%d [UP/DN]",nt,offset+1,offset+6<nt?offset+6:nt);text(176,345,buf,5,1);
for(i=0;i<6;i++)if(rows[i].name[0]){text(26,363+i*14,rows[i].name,1,1);sprintf(buf,"%s %4d",rows[i].state==TS_RUN?"RUN ":rows[i].state==TS_READY?"READY":"WAIT ",rows[i].pri);text(382,363+i*14,buf,5,1);}
iw=w->Width-w->BorderLeft-w->BorderRight;ih=w->Height-w->BorderTop-w->BorderBottom;if(iw>0&&ih>0&&iw<=sc->Width&&ih<=sc->Height){rw=iw;rh=iw*H/W;if(rh>ih){rh=ih;rw=ih*W/H;}ox=(iw-rw)/2;oy=(ih-rh)/2;memset(out,(UBYTE)pens[0],(ULONG)iw*ih);for(y=0;y<rh;y++){int sy=(ULONG)y*H/rh;for(x=0;x<rw;x++)out[(ULONG)(oy+y)*iw+ox+x]=frame[sy*W+(ULONG)x*W/rw];}WriteChunkyPixels(w->RPort,w->BorderLeft,w->BorderTop,w->BorderLeft+iw-1,w->BorderTop+ih-1,out,iw);}
Delay(11+(step++%3));
}
end:
if(irqon)RemIntServer(INTB_VERTB,&irq);if(taskon)RemTask(probe);if(stack)FreeMem(stack,8192);if(probe)FreeMem(probe,sizeof(struct Task));
if(w)CloseWindow(w);if(sc){for(i=0;i<got;i++)ReleasePen(sc->ViewPort.ColorMap,pens[i]);UnlockPubScreen(0,sc);}free(face);free(frame);free(font);free(out);if(GfxBase)CloseLibrary((struct Library*)GfxBase);if(IntuitionBase)CloseLibrary((struct Library*)IntuitionBase);return 0;
}