- Prettify textual simulation output, simplyfing the code, too.

- Fix return code on exit


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@844 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-08-12 13:33:28 +00:00
parent 97d6f574af
commit 51fdeea4d1
2 changed files with 87 additions and 92 deletions

View File

@ -132,6 +132,11 @@ parse_options(int argc, char** argv)
TextSimulation sim; TextSimulation sim;
std::string str; std::string str;
std::cout << std::endl
<< _(" [II] To see a list of commands available,\n"
" [II] please type \"help\" and hit the ENTER key.")
<< std::endl;
std::cout << std::endl << "% "; std::cout << std::endl << "% ";
while (getline(std::cin, str)) while (getline(std::cin, str))
{ {

View File

@ -34,6 +34,9 @@
#include "text_simulation.hh" #include "text_simulation.hh"
#include <cassert>
#include <ios>
#include <iomanip>
#include <sstream> #include <sstream>
@ -574,10 +577,10 @@ TextSimulation::on_quit(const Tokens& arguments)
if (!unsaved_ask_confirm()) if (!unsaved_ask_confirm())
return; return;
p_stdout(_("\n\n*** Thank you for using SGPEM by Sirius Cybernetics Corporation ***\n\n")); p_stdout(_("\nBye.\n\n"));
// Is this ok? Really? Oh, sure, if it we always did it in this way, it is surely a Good Thing! // Exit the program with a correct return code.
exit(1); exit(0);
} }
void void
@ -1476,19 +1479,19 @@ operator<<(ostream& os, Schedulable::state state)
switch (state) switch (state)
{ {
case Schedulable::state_running: case Schedulable::state_running:
os << " RUNNING @"; os << _("RUNNING @");
break; break;
case Schedulable::state_ready: case Schedulable::state_ready:
os << " READY "; os << _("READY");
break; break;
case Schedulable::state_blocked: case Schedulable::state_blocked:
os << " BLOCKED "; os << _("BLOCKED");
break; break;
case Schedulable::state_future: case Schedulable::state_future:
os << " FUTURE "; os << _("FUTURE");
break; break;
case Schedulable::state_terminated: case Schedulable::state_terminated:
os << " TERMINATED "; os << _("TERMINATED");
break; break;
default: default:
os.setstate(ios_base::failbit); os.setstate(ios_base::failbit);
@ -1503,19 +1506,19 @@ operator<<(ostream& os, Request::state state)
switch (state) switch (state)
{ {
case Request::state_unallocable: case Request::state_unallocable:
os << "UNALLOCABLE"; os << _("UNALLOCABLE");
break; break;
case Request::state_allocated: case Request::state_allocated:
os << " ALLOCATED"; os << _("ALLOCATED");
break; break;
case Request::state_future: case Request::state_future:
os << " FUTURE"; os << _("FUTURE");
break; break;
case Request::state_exhausted: case Request::state_exhausted:
os << " EXHAUSTED"; os << _("EXHAUSTED");
break; break;
case Request::state_allocable: case Request::state_allocable:
os << " ALLOCABLE"; os << _("ALLOCABLE");
break; break;
default: default:
os.setstate(ios_base::failbit); os.setstate(ios_base::failbit);
@ -1528,19 +1531,21 @@ void
TextSimulation::update(const History& changed_history) TextSimulation::update(const History& changed_history)
{ {
ostringstream oss; ostringstream oss;
int printed_instant; int printed_instant;
static const std::string tab = " ";
// Print header for each instant:
if (changed_history.get_size() > 1) if (changed_history.get_size() > 1)
printed_instant = static_cast<int>(changed_history.get_size()) - 2; printed_instant = static_cast<int>(changed_history.get_size()) - 2;
else else
printed_instant = -1; printed_instant = -1;
oss << ">>>> " << printed_instant << _("\nREADY QUEUE: { "); oss << endl << ">>>> " << printed_instant;
p_stdout(oss.str());
oss.str(string()); // Print ready queue
oss << endl << _("READY QUEUE: { ");
const Environment& env = changed_history.get_last_environment(); const Environment& env = changed_history.get_last_environment();
const ReadyQueue& q = env.get_sorted_queue(); const ReadyQueue& q = env.get_sorted_queue();
@ -1548,62 +1553,36 @@ TextSimulation::update(const History& changed_history)
for (unsigned int i = 0; i < q.size(); ++i) for (unsigned int i = 0; i < q.size(); ++i)
{ {
const Thread& t = q.get_item_at(i); const Thread& t = q.get_item_at(i);
oss << t.get_name() + _(" ~ ");
p_stdout(t.get_name() + " ~ ");
} }
p_stdout("}\n"); oss << _("}") << endl;
// Flush buffer to screen
p_stdout(oss.str());
oss.str(string());
const Environment::Resources& resources = env.get_resources(); const Environment::Resources& resources = env.get_resources();
const Environment::Processes& processes = env.get_processes(); const Environment::Processes& processes = env.get_processes();
typedef Environment::Resources::const_iterator ResourceIt; typedef Environment::Resources::const_iterator ResourceIt;
std::string tab = " "; // Write the queue of requests for each resource
std::string::size_type max = tab.size(); oss << _("RESOURCES:") << endl;
for (unsigned int pi = 0; pi < processes.size(); ++pi)
{
Process& p = *processes[pi];
if (p.get_name().size() > max + tab.size() + tab.size())
max = p.get_name().size() - tab.size() - tab.size();
vector<Thread*> threads = p.get_threads();
for (unsigned int ti = 0; ti < threads.size(); ++ti)
{
Thread& t = *threads[ti];
if (t.get_name().size() > max + tab.size())
max = t.get_name().size() - tab.size();;
}
for (ResourceIt it = resources.begin(); it != resources.end(); ++it)
{
const Resource& r = *it->second;
if (r.get_name().size() > max)
max = r.get_name().size();
}
}
p_stdout(_("RESOURCES:\n"));
for (ResourceIt it = resources.begin(); it != resources.end(); ++it) for (ResourceIt it = resources.begin(); it != resources.end(); ++it)
{ {
const Resource& r = *it->second; const Resource& r = *it->second;
Environment::resource_key_t key = it->first; Environment::resource_key_t key = it->first;
oss << " " << (key < 10 ? " " : "") << key << ". " << r.get_name() << _(", with "); oss << right << setw(3) << key << left << ". " << r.get_name() << _(", with ");
oss << r.get_places() << _(" places") << endl;
oss << r.get_places() << _(" places\n");
p_stdout(oss.str());
oss.str(string());
const Environment::SubRequestQueue& req_queue = const Environment::SubRequestQueue& req_queue =
env.get_request_queue(it->first); env.get_request_queue(it->first);
p_stdout(_("\t\t\tqueue: { ")); oss << setw(tab.size()*3) << ' ' << _("queue: { ");
for (unsigned int i = 0; i < req_queue.size(); ++i) for (unsigned int i = 0; i < req_queue.size(); ++i)
{ {
@ -1617,33 +1596,48 @@ TextSimulation::update(const History& changed_history)
oss << "[" << req_queue[i]->get_request().get_thread().get_name() << "]"; oss << "[" << req_queue[i]->get_request().get_thread().get_name() << "]";
else else
oss << req_queue[i]->get_request().get_thread().get_name(); oss << req_queue[i]->get_request().get_thread().get_name();
}
oss << _(" }") << endl;
}
// Flush buffer to screen
p_stdout(oss.str()); p_stdout(oss.str());
oss.str(string()); oss.str(string());
}
p_stdout(" }\n");
}
// Set new format state flags, and save the old one
static const unsigned int fill0 = 25;
static const unsigned int fill1 = 15;
// Minimum fill0 for the usage we make of it below:
assert(fill0 > tab.size()*2 + 6);
p_stdout(_("\nPROCESSES:")); oss << endl;
Glib::ustring space_bar_1 = " ";
space_bar_1.resize(max, ' '); oss << left;
p_stdout(space_bar_1); oss << setw(fill0) << _("PROCESSES:");
p_stdout(_(" state arrival requiring elapsed priority resource_id\n")); oss << right;
oss << setw(fill1) << _("state")
<< setw(fill1) << _("arrival")
<< setw(fill1) << _("requiring")
<< setw(fill1) << _("elapsed")
<< setw(fill1) << _("priority")
<< setw(fill1) << _("resource_id")
<< endl;
for (unsigned int pi = 0; pi < processes.size(); ++pi) for (unsigned int pi = 0; pi < processes.size(); ++pi)
{ {
Process& p = *processes[pi]; Process& p = *processes[pi];
Glib::ustring upname(p.get_name()); oss << setw(2) << right << (pi + 1) << left << ". "
upname.resize(max + tab.size() + tab.size(), ' '); << setw(fill0 - 4) << p.get_name().substr(0, fill0 - 5);
oss << " " << (pi < 9 ? " " : "") << pi + 1 << ". " << upname; oss << right;
oss << " " << p.get_state(); oss << setw(fill1) << p.get_state();
oss << _(" ") << (p.get_arrival_time() < 10 ? " " : "") << p.get_arrival_time(); oss << setw(fill1) << p.get_arrival_time();
oss << _(" ") << (p.get_total_cpu_time() < 10 ? " " : "") << p.get_total_cpu_time(); oss << setw(fill1) << p.get_total_cpu_time();
oss << _(" ") << (p.get_elapsed_time() < 10 ? " " : "") << p.get_elapsed_time(); oss << setw(fill1) << p.get_elapsed_time();
oss << _(" ") << (p.get_current_priority() < 10 ? " " : "") << p.get_current_priority() << endl; oss << setw(fill1) << p.get_current_priority();
oss << endl;
p_stdout(oss.str()); p_stdout(oss.str());
oss.str(string()); oss.str(string());
@ -1654,14 +1648,15 @@ TextSimulation::update(const History& changed_history)
{ {
Thread& t = *threads[ti]; Thread& t = *threads[ti];
Glib::ustring upname(t.get_name()); oss << right << setw(tab.size() + 2) << ti + 1 << left << ". "
upname.resize(max + tab.size(), ' '); << setw(fill0 - 4 - tab.size()) << t.get_name().substr(0, fill0 - 5 - tab.size());
oss << " " << tab << (ti < 9 ? " " : "") << ti + 1 << ". " << upname; oss << right;
oss << " " << t.get_state(); oss << setw(fill1) << t.get_state();
oss << _(" ") << (t.get_arrival_time() < 10 ? " " : "") << t.get_arrival_time(); oss << setw(fill1) << t.get_arrival_time();
oss << _(" ") << (t.get_total_cpu_time() < 10 ? " " : "") << t.get_total_cpu_time(); oss << setw(fill1) << t.get_total_cpu_time();
oss << _(" ") << (t.get_elapsed_time() < 10 ? " " : "") << t.get_elapsed_time(); oss << setw(fill1) << t.get_elapsed_time();
oss << _(" ") << (t.get_current_priority() < 10 ? " " : "") << t.get_current_priority() << endl; oss << setw(fill1) << t.get_current_priority();
oss << endl;
p_stdout(oss.str()); p_stdout(oss.str());
oss.str(string()); oss.str(string());
@ -1679,18 +1674,15 @@ TextSimulation::update(const History& changed_history)
SubRequest& sr = *subrequests[sri]; SubRequest& sr = *subrequests[sri];
ResourceIt point = resources.find(sr.get_resource_key()); ResourceIt point = resources.find(sr.get_resource_key());
oss << " " << tab << " "; oss << setw(2 + tab.size()*2) << ri + 1 << left << "." << setw(2) << sri + 1
oss << (ri < 9 ? " " : "") << ri + 1 << "." << sri + 1 << (sri < 9 ? " " : ""); << setw(fill0 - 5 - tab.size()*2) << (point->second)->get_name().substr(0, fill0 - 6 - tab.size()*2);
oss << right;
Glib::ustring upname((point->second)->get_name()); oss << setw(fill1) << sr.get_state();
upname.resize(max, ' '); oss << setw(fill1) << r.get_instant();
oss << _(" ") << upname; oss << setw(fill1) << sr.get_length();
oss << " " << sr.get_state(); oss << setw(fill1) << sr.get_length() - sr.get_remaining_time();
oss << _(" ") << (r.get_instant() < 10 ? " " : "") << r.get_instant(); oss << setw(fill1) << sr.get_resource_key();
oss << _(" ") << (sr.get_length() < 10 ? " " : "") << sr.get_length(); oss << endl;
oss << _(" ") << (sr.get_length() - sr.get_remaining_time() < 10 ? " " : "") << sr.get_length() - sr.get_remaining_time();
oss << _(" ") << sr.get_resource_key() << " \n";
p_stdout(oss.str()); p_stdout(oss.str());
oss.str(string()); oss.str(string());
@ -1699,7 +1691,5 @@ TextSimulation::update(const History& changed_history)
} }
} }
p_stdout("\n");
} }