Very heavy CPU Usage

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Fatal1ty
Posts: 2
Joined: Sun Jan 13, 2008 6:52 am

Very heavy CPU Usage

Post by Fatal1ty »

Hi there!

First of all, I'm from Germany so please be patient about my bad english, I hope it's enough. Maybe you can say me how my text was :wink:

Now to my problem, I'm writing on a Netstream Player, I use FMOD for playing the stream, and now I wanted to use Irrlicht for the Interface, but when I now start my programm, I've 99% CPU usage on both cores. That can't be normal. And without the Irrlicht Interface I've 10% at maximum.

I think the best would be when I show you my code, there is many German text, but I hope you can understand it anyway
Here's the main.cpp:

Code: Select all

 
/*===============================================================================================
Technobase.FM Player - Spielt den Technobase.FM MP3 Stream ab und bietet eine Community Platform
 
TO-DO:
 
-Interface
-ACC Stream abspielen können
-Sämtliche Features
===============================================================================================*/
#include "main.h"
 
///Mainwindow erstellen
int WINAPI WinMain( HINSTANCE, HINSTANCE, PSTR, int )
{ 
 
    ///Irrlicht Device + Irrlicht System erstellen
 
    device = createDevice(EDT_OPENGL, dimension2d<s32>(320, 240), 16, false, false, false, 0);
 
    device->setWindowCaption(L"TechnoBase.FM Player - 2008 by Christian Sauthoff");
 
    driver = device->getVideoDriver();
    smgr = device->getSceneManager();
    guienv = device->getGUIEnvironment();
    //filesys = device->getFileSystem();
 
    ///Interface erstellen
 
    Prozentanzeige = guienv->addStaticText(L"We aRe oNe!",rect<s32>(10, 10, 10+100, 10+20));
 
 
 
    ///FMOD Initialiesieren
 
    FSOUND_Init(32000, 64, 0);
    FSOUND_Stream_SetBufferSize(100);
    FSOUND_Stream_Net_SetBufferProperties(64000, 60, 80);
    TechnobaseFM_Stream = FSOUND_Stream_Open("http://dsl.tb-stream.net:80", FSOUND_NORMAL | FSOUND_NONBLOCKING, 0, 0);
 
 
 
    ///Hauptschleife
    while(device->run())
    {
        driver->beginScene(true, true, SColor(255,255,255,255)); ///Irrlicht Szene Beginnen
 
 
        //UpdateKeys();
        UpdateStream();
 
        Prozentanzeige->setText(L"");
        ProzentanzeigeStr=L"";
        ProzentanzeigeStr.append(stringw(GetStreamPercent()));
        Prozentanzeige->setText(ProzentanzeigeStr.c_str());
 
 
        smgr->drawAll();
        guienv->drawAll();
        driver->endScene(); ///Irrlicht Szene beenden
 
    }
 
    ///Programm beenden
    device->drop();
 
    FSOUND_Stream_Close(TechnobaseFM_Stream);
    FSOUND_Close();
 
 
}
 
 
 
 
 
 
 
 
 
 
 
///========================================================================================================================
///==================================================FUNKTIONEN============================================================
///========================================================================================================================
 
 
int GetStreamStatus()
{
    FSOUND_Stream_Net_GetStatus(TechnobaseFM_Stream, &StreamStatus, &Prozent_Geladen, &StreamBitrate, &Stream_Flags);
    return 5;
}
 
int GetStreamPercent()
{
    FSOUND_Stream_Net_GetStatus(TechnobaseFM_Stream, &StreamStatus, &Prozent_Geladen, &StreamBitrate, &Stream_Flags);
    return Prozent_Geladen;
}
 
 
 
void UpdateKeys()
{
 
    ///Tastenabfragen
    if (kbhit())
    {
        LetzteTaste = getch();
 
        if (LetzteTaste == ' ')
        {
            if(PauseAktiv) {
                FSOUND_SetPaused(TechnobaseFM_Channel, false);
                PauseAktiv=false;
            }
            else if(!PauseAktiv) {
                FSOUND_SetPaused(TechnobaseFM_Channel, true);
                PauseAktiv=true;
            }
        }
    }
 
 
}
 
void UpdateStream()
{
 
        if (TechnobaseFM_Channel < 0 && Prozent_Geladen>90)  ///Wenn Noch nicht abgespielt, und genug vorgebuffert abspielen
        {
            TechnobaseFM_Channel = FSOUND_Stream_PlayEx(FSOUND_FREE, TechnobaseFM_Stream, NULL, TRUE);
            FSOUND_SetPaused(TechnobaseFM_Channel, FALSE);
        }
 }
 
And the header file:

Code: Select all

 
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
 
//=========================================================================================================================
//==================================ANFANG INCLUDIEREN DER HEADER DATEIEN==================================================
//=========================================================================================================================
#include <irrlicht.h>
 
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
 
#include <fmod.h>
#include <fmod_errors.h>
 
#include <iostream>
//=========================================================================================================================
//=====================================ENDE INCLUDIEREN DER HEADER DATEIEN=================================================
//=========================================================================================================================
 
 
 
 
 
//=========================================================================================================================
//===========================================ANFANG SETZEN DER NAMESPACES==================================================
//=========================================================================================================================
using namespace irr;
 
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
using namespace std;
//=========================================================================================================================
//==========================================ENDE SETZEN DER NAMESPACES=====================================================
//=========================================================================================================================
 
 
 
//=========================================================================================================================
//===========================================ANFANG FUNKTIONSDEKLARIERUNG==================================================
//=========================================================================================================================
int GetStreamStatus();
int GetStreamPercent();
 
void UpdateKeys();
void UpdateStream();
 
//=========================================================================================================================
//============================================ENDE FUNKTIONSDEKLARIERUNG===================================================
//=========================================================================================================================
 
 
 
 
//=========================================================================================================================
//===========================================ANFANG VARIABLENDEKLARIERUNG==================================================
//=========================================================================================================================
//FMOD Variablen
int Prozent_Geladen = 0, StreamStatus = 0, StreamZustand, StreamBitrate;
unsigned int Stream_Flags;
int TechnobaseFM_Channel = -1;
 
//Interface Variablen
char LetzteTaste=0;
 
bool PauseAktiv=false;
 
stringw ProzentanzeigeStr;
//=========================================================================================================================
//============================================ENDE VARIABLENDEKLARIERUNG===================================================
//=========================================================================================================================
 
 
 
 
 
//=========================================================================================================================
//===========================================ANFANG ERSTELLEN DER POINTER==================================================
//=========================================================================================================================
 
//Irrlicht / Interface Pointer
IrrlichtDevice *device = 0;
IVideoDriver* driver = 0;
ISceneManager* smgr = 0;
IGUIEnvironment* guienv = 0;
IFileSystem* filesys = 0;
 
IGUIStaticText* Prozentanzeige = 0;
 
//FMOD/NetStream Pointer
FMUSIC_MODULE *mod = NULL;
FSOUND_STREAM *TechnobaseFM_Stream;
 
//=========================================================================================================================
//============================================ENDE ERSTELLEN DER POINTER===================================================
//=========================================================================================================================
 
 
 
 
 
#endif // MAIN_H_INCLUDED
 

Why is the CPU Usage so high?

I hope you can help me,

Thanks,

Christian
Last edited by Fatal1ty on Mon Sep 02, 2013 8:32 pm, edited 1 time in total.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

It's normal i think, but not a problem.... If you wanted to run other things then they'd run fine.

You can put a sleep in the render loop i think, just a short sleep and it will bring the CPU usage down, but i'm not sure it's necessary.

DISCLAIMER: I think i'm not talking absolute 'quatsch'... ;)
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Code: Select all

    while(device->run())
    {
        driver->beginScene(true, true, SColor(255,255,255,255)); ///Irrlicht Szene Beginnen


        //UpdateKeys();
        UpdateStream();

        Prozentanzeige->setText(L"");
        ProzentanzeigeStr=L"";
        ProzentanzeigeStr.append(stringw(GetStreamPercent()));
        Prozentanzeige->setText(ProzentanzeigeStr.c_str());


        smgr->drawAll();
        guienv->drawAll();
        driver->endScene(); ///Irrlicht Szene beenden

    } 
Question: why would you expect this (or any other) busy loop to use less than 100% CPU?

Why both cores are maxed out is less clear. I'd guess that FSOUND spawns another thread, which is signalled by FSOUND_Stream_PlayEx().

Either way, a sleep in your busy loop should sort it out.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Fatal1ty
Posts: 2
Joined: Sun Jan 13, 2008 6:52 am

Post by Fatal1ty »

Oh,

many many thanks! A little Sleep and all my problems were solved :D

thanks

Christian

@JP: Nein, du hast nich kompletten Quatsch geredet, hat super funktioniert und war genau das richtige! :P
Frodenius
Posts: 64
Joined: Sun Aug 29, 2004 11:24 am
Location: Germany/Frankfurt
Contact:

Post by Frodenius »

[werbung]
die antwort hättste auch im deutschen forum gekriegt, wa? ;)
[/werbung]
worst programming style ever seen...
Post Reply