New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chaiscript cpu usage steadily grows up if executing mainloop from chaiscript. #394

Open
germandiagogomez opened this Issue Dec 28, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@germandiagogomez

germandiagogomez commented Dec 28, 2017

  • Compiler Used:

clang

  • Operating System:

Mac OS High Sierra

  • Architecture (ARM/x86/32bit/64bit/etc):

x86_64

Expected Behavior

CPU usage should be stable.

Actual Behavior

CPU usage grows as time goes on.

Minimal Example to Reproduce Behavior

I have a task manager for concurrency inside .chai scripts. This task manager is a c++ object and the instance lives in c++ side and is exposed in ChaiScript. It executes Tasks. The weird problem is the following: if I execute taskscheduler.run() from inside ChaiScript the cpu usage grows indefinitely. If I do it from C++ side (just change the run in the script for the run in C++ in the example below), then things are stable.

More information:

  • making a normal while loop (without the task scheduler) with exactly the same code to execute does not grow cpu usage, it is stable.
  • creating an instance of TaskScheduler in ChaiScript side and calling taskscheduler.run() shows exactly the same problem.

How to reproduce (example below that can be compiled using c++14):

  1. add chaiscript function to scheduler from ChaiScript. This implies that the function is created as a chaiscript function, not a c++ function.
  2. run the TaskScheduler::run (it is a c++ instance of a type, not a chaiscript type) from inside chaiscript.

If you TaskScheduler::run in C++, things are ok. If you run TaskScheduler::run in ChaiScript, the problem shows.

Below it follows a complete example:

#include <chaiscript/chaiscript.hpp>
#include <functional>
#include <algorithm>
#include <thread>

using namespace std;
using namespace chaiscript;

struct Task {
    std::function <bool (int)> f;

    bool operator()(int e) {
        return f(e);
    }
};

class TaskScheduler {
    std::list<Task> theTasks_;
public:
    void addTask(std::function<bool (int)> f) {
        Task t{std::move(f)};
        auto it = upper_bound(theTasks_.begin(),
                    theTasks_.end(),
                    t, [](Task const &, Task const &) {
                        return true;
                    });
        theTasks_.insert(it, Task{std::move(t)});
    }

    void run() {
        while (true) {
            this_thread::sleep_for(10ms);
            for (auto taskIt = theTasks_.begin(); taskIt != theTasks_.end(); ++taskIt) {
                if ((*taskIt)(10)) {
                }
            }
        }
    }
};

TaskScheduler ts;


constexpr char const * chaiCode =
    R"--chai(
    var num = 10
    var num2 = 20
    var num3 = 30
    var num4 = 30
    var num5 = 30
    var num6 = 30
    var num7 = 30
    var chaiFunc = fun[num, num2, num3, num4, num5, num6, num7](elapsed) {
         num.to_string()
         num2.to_string()
         num3.to_string()
         num4.to_string()
         num5.to_string()
         num6.to_string()
         num7.to_string()
         return false
    }

    ts.addTask(chaiFunc)

    //This will loop and start to eat more and more cpu as time goes. Comment this and 
    //uncomment the run() in C++ side and the problem disappears
    ts.run() 
 )--chai";

int main() {
    ChaiScript chai;

    chai.add(user_type<TaskScheduler>(), "TaskScheduler");
    chai.add(fun(&TaskScheduler::addTask), "addTask");
    chai.add(fun(&TaskScheduler::run), "run");
    chai.add_global(var(ts), "ts");
    chai.eval(chaiCode);

   //Uncomment this and comment the run() in chaiscript side to see the problem disappear.
   // ts.run();
}
@ninnghazad

This comment has been minimized.

Show comment
Hide comment
@ninnghazad

ninnghazad Mar 5, 2018

Contributor

Confirmed using g++ 7.2 on debian with ChaiScript 6.0.0.
CPU usage slowly rises up to ~75% for one Core.
Calling ts.run the alternative way as mentioned above, usage stays below 2%.

Contributor

ninnghazad commented Mar 5, 2018

Confirmed using g++ 7.2 on debian with ChaiScript 6.0.0.
CPU usage slowly rises up to ~75% for one Core.
Calling ts.run the alternative way as mentioned above, usage stays below 2%.

@ninnghazad

This comment has been minimized.

Show comment
Hide comment
@ninnghazad

ninnghazad Mar 24, 2018

Contributor

just wondered - does chai with/out threading affect this? manually inserting timers in run() - which part is actually slow? could the sleep mess things up? or is it the chai-calls-c++-calls-chai-calls-c++ ?

Contributor

ninnghazad commented Mar 24, 2018

just wondered - does chai with/out threading affect this? manually inserting timers in run() - which part is actually slow? could the sleep mess things up? or is it the chai-calls-c++-calls-chai-calls-c++ ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment