Skip to content

Commit

Permalink
feat-n-fixes: fixes p6 test and adds new test for cache misses
Browse files Browse the repository at this point in the history
  • Loading branch information
anthongretter committed Dec 9, 2024
1 parent c2f4372 commit 881a9c9
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 17 deletions.
112 changes: 112 additions & 0 deletions app/hidenseek/hidenseek.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <utility/ostream.h>
#include <process.h>
#include <time.h>
#include <real-time.h>
#include <utility/random.h>

#define SPACE_SIZE 1000

using namespace EPOS;

OStream cout;
Mutex cout_m;
Mutex space_m;

int space[SPACE_SIZE];
int total_hids = 0;
int total_found = 0;

int hider(int tid);
int seeker(int tid);


int main()
{
cout << "MAIN: Hello world!\n" << endl;

const int N_THREADS_P = 5; // Periodic

int (*routines[2])(int) = {&hider, &seeker};
Thread *ts[N_THREADS_P];

for (int i = 0; i < N_THREADS_P; i++)
{
// Thread periodic
auto conf = Periodic_Thread::Configuration(
500000 + (unsigned(Random::random()) % 500000), // 0.5s - 1s
(500000 + (unsigned(Random::random()) % 500000)) * 4,
Periodic_Thread::UNKNOWN,
Periodic_Thread::NOW,
5
);
ts[i] = new Periodic_Thread(conf, routines[i % 2], i);
}

for (int i = 0; i < N_THREADS_P; i++) {
ts[i]->join();
}

cout << "\nMAIN: " << "hidden: " << total_hids << ", found: " << total_found << endl;
if (total_found < total_hids) {
cout << "MAIN: " << "what a shame :(" << endl;
}

cout << "MAIN: Bye!" << endl;

return 0;
}


int hider(int tid)
{
do {
cout_m.lock();
cout << "<" << CPU::id() << ">: " << "h" << tid << ": hiding..." << endl;
cout_m.unlock();

int random_place_to_hide = unsigned(Random::random()) % SPACE_SIZE;

space_m.lock();
space[random_place_to_hide] = 42;
total_hids++;
space_m.unlock();

cout_m.lock();
cout << "<" << CPU::id() << ">: " << "h" << tid << ": bye!" << endl;
cout_m.unlock();
} while (Periodic_Thread::wait_next());
return 0;
}


int seeker(int tid)
{
do {
cout_m.lock();
cout << "<" << CPU::id() << ">: " << "s" << tid << ": seeking..." << endl;
cout_m.unlock();

bool found = false;

// grandes pulos de acesso para causar cache miss
for (int i = 0; i < SPACE_SIZE; i += (unsigned(Random::random()) % (SPACE_SIZE / 10))) {
space_m.lock();
int searched = space[i];
found = searched == 42;
space[i] = 0;
space_m.unlock();

if (found) {
cout_m.lock();
total_found++;
cout << "<" << CPU::id() << ">: " << "s" << tid << ": FOUND ONE! at " << i << endl;
cout_m.unlock();
}
}

cout_m.lock();
cout << "<" << CPU::id() << ">: " << "s" << tid << ": bye!" << endl;
cout_m.unlock();
} while (Periodic_Thread::wait_next());
return 0;
}
205 changes: 205 additions & 0 deletions app/hidenseek/hidenseek_traits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#ifndef __traits_h
#define __traits_h

#include <system/config.h>

__BEGIN_SYS

// Build
template <>
struct Traits<Build> : public Traits_Tokens
{
// Basic configuration
static const unsigned int SMOD = LIBRARY;
static const unsigned int ARCHITECTURE = IA32;
static const unsigned int MACHINE = PC;
static const unsigned int MODEL = Legacy_PC;
static const unsigned int CPUS = 4;
static const unsigned int NETWORKING = STANDALONE;
static const unsigned int EXPECTED_SIMULATION_TIME = 60; // s (0 => not simulated)

// Default flags
static const bool enabled = true;
static const bool monitored = true;
static const bool debugged = true;
static const bool hysterically_debugged = false;
};

// Utilities
template <>
struct Traits<Debug> : public Traits<Build>
{
static const bool error = true;
static const bool warning = true;
static const bool info = false;
static const bool trace = false;
};

template <>
struct Traits<AAA> : public Traits<Build>
{
static const bool debugged = true;
};

template <>
struct Traits<PEAMQ> : public Traits<Build>
{
static const bool debugged = false;
};

template <>
struct Traits<EAMQ> : public Traits<Build>
{
static const bool debugged = false;
};

template <>
struct Traits<GEAMQ> : public Traits<Build>
{
static const bool debugged = false;
};

template <>
struct Traits<Lists> : public Traits<Build>
{
static const bool debugged = false;
};

template <>
struct Traits<Spin> : public Traits<Build>
{
static const bool debugged = false;
};

template <>
struct Traits<Heaps> : public Traits<Build>
{
static const bool debugged = false;
};

template <>
struct Traits<Observers> : public Traits<Build>
{
// Some observed objects are created before initializing the Display
// Enabling debug may cause trouble in some Machines
static const bool debugged = false;
};

// System Parts (mostly to fine control debugging)
template <>
struct Traits<Boot> : public Traits<Build>
{
static const bool debugged = false;
};

template <>
struct Traits<Setup> : public Traits<Build>
{
static const bool debugged = false;
};

template <>
struct Traits<Init> : public Traits<Build>
{
static const bool debugged = false;
};

template <>
struct Traits<Framework> : public Traits<Build>
{
static const bool debugged = false;
};

template <>
struct Traits<Aspect> : public Traits<Build>
{
static const bool debugged = hysterically_debugged;
};

__END_SYS

// Mediators
#include __ARCHITECTURE_TRAITS_H
#include __MACHINE_TRAITS_H

__BEGIN_SYS

// API Components
template <>
struct Traits<Application> : public Traits<Build>
{
static const unsigned int STACK_SIZE = Traits<Machine>::STACK_SIZE;
static const unsigned int HEAP_SIZE = Traits<Machine>::HEAP_SIZE;
static const unsigned int MAX_THREADS = Traits<Machine>::MAX_THREADS;
};

template <>
struct Traits<System> : public Traits<Build>
{
static const bool multithread = (Traits<Application>::MAX_THREADS > 1) || (CPUS > 1);
static const bool multicore = multithread && (CPUS > 1);
static const bool multiheap = false;

static const unsigned long LIFE_SPAN = 1 * YEAR; // s
static const unsigned int DUTY_CYCLE = 1000000; // ppm

static const bool reboot = true;

static const unsigned int STACK_SIZE = Traits<Machine>::STACK_SIZE;
static const unsigned int HEAP_SIZE = (Traits<Application>::MAX_THREADS + Traits<Build>::CPUS) * Traits<Application>::STACK_SIZE;

static const unsigned int RUN_TO_HALT = false;

};

template <>
struct Traits<Thread> : public Traits<Build>
{
static const bool enabled = Traits<System>::multithread;
static const bool smp = Traits<System>::multicore;
static const bool trace_idle = hysterically_debugged;
static const bool simulate_capacity = false;
static const int priority_inversion_protocol = NONE;


typedef IF<(CPUS > 1), PEAMQ, EAMQ>::Result Criterion;
static const unsigned int QUANTUM = 10000; // us

static const bool debugged = false;
};

template <>
struct Traits<Scheduler<Thread>> : public Traits<Build>
{
static const bool debugged = false;
};

template <>
struct Traits<Synchronizer> : public Traits<Build>
{
static const bool enabled = Traits<System>::multithread;
static const bool debugged = false;
};

template <>
struct Traits<Alarm> : public Traits<Build>
{
static const bool visible = hysterically_debugged;
static const bool debugged = false;
};

template <>
struct Traits<Address_Space> : public Traits<Build>
{
static const bool debugged = false;
};

template <>
struct Traits<Segment> : public Traits<Build>
{
static const bool debugged = false;
};

__END_SYS

#endif
17 changes: 17 additions & 0 deletions app/hidenseek/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EPOS Application Makefile

include ../../makedefs

all: install

$(APPLICATION): $(APPLICATION).o $(LIB)/*
$(ALD) $(ALDFLAGS) -o $@ $(APPLICATION).o

$(APPLICATION).o: $(APPLICATION).cc $(SRC)
$(ACC) $(ACCFLAGS) -o $@ $<

install: $(APPLICATION)
$(INSTALL) $(APPLICATION) $(IMG)

clean:
$(CLEAN) *.o $(APPLICATION)
10 changes: 5 additions & 5 deletions app/p6/p6.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ int poggers(int n)
cout << "CPU: " << CPU::id() << " poggers - " << n << endl;
cout_m.unlock();

const int size = 1000000; // Adjust size as needed
volatile int arr[size];
const int size = 100;
int arr[size];

// grandes pulos de acesso para causar cache miss
for (int i = 0; i < size; i += (unsigned(Random::random()) % 100)) {
for (int i = 0; i < size; i += (unsigned(Random::random()) % 10)) {
// esconde surpresa
arr[i] = 42;
}
Expand All @@ -49,7 +49,7 @@ int main()
{
cout << "MAIN: Hello world!\n" << endl;

const int N_THREADS_A = 0; // Aperiodic
const int N_THREADS_A = 12; // Aperiodic
const int N_THREADS_P = 4; // Periodic

const Thread::Criterion CRITS[3]{Thread::LOW, Thread::NORMAL, Thread::HIGH};
Expand All @@ -64,7 +64,7 @@ int main()
(500000 + (unsigned(Random::random()) % 500000)) * 4,
Periodic_Thread::UNKNOWN,
Periodic_Thread::NOW,
10
2
);
ts[i] = new Periodic_Thread(conf, &poggers, i);
}
Expand Down
2 changes: 0 additions & 2 deletions include/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,6 @@ class PEAMQ : public Variable_Queue_Scheduler, public EAMQ
// P7 : booleano para indicar se permite migrar thread ou não
static const bool migration = true;

// P6 : Core Statistics para threads periódicas
PEAMQ(int p = APERIODIC)
: Variable_Queue_Scheduler(((p == IDLE) || (p == MAIN)) ? CPU::id() : ++_next_queue %= CPU::cores()), EAMQ(p) {}
PEAMQ(const Microsecond & p, const Microsecond & d = SAME, const Microsecond & c = UNKNOWN, unsigned int cpu = ANY)
Expand Down Expand Up @@ -537,7 +536,6 @@ class PEAMQ : public Variable_Queue_Scheduler, public EAMQ
protected:
volatile unsigned int evaluate(bool max_core=false);
static Core_Statistics _core_statistics;
static Spin _core_lock;

};

Expand Down
Loading

0 comments on commit 881a9c9

Please sign in to comment.