c++ - Two phase construction at real time system -
i developing real time system, , debating design of classes.
specific, can't decide whether build "heavy" classes using 2 phase construction.
on 1 hand, calling constructor of "heavy" class can major bottle-neck @ running time, , saves me creating classes , allocating memory of features user might won't use.
on other hand, 2 phase construction can makes surprises during execution, considering situation when try access ability, can't since didn't initialize, , need build before using.
my tendency go 2 phase construction method. hear pros\cons 2 phase construction @ real time system. , if there better approach toward this.
here code example of heavy class (my classes sure won't that, demonstrate idea):
class veryheavy { private: heavyclass1* p1; heavyclass2* p2; heavyclass3* p3; heavyclass4* p4; heavyclass5* p5; int* hugearray [100000]; //...// };
this agc, apollo guidance computer, used both on apollo command module , lunar module. famous causing apollo 11 landing scrubbed. right in middle of descent moon surface, computer crashed on real-time error. several times. producing system error 1201 (executive overflow - no vacant areas) , system error 1202 (executive overflow - no core sets). armstrong , aldrin saw number, ui device see on right of photo primitive show strings. guidance controller steve bales knew numbers meant (they had never seen error while training) , knew system recover it. , saved landing giving go anyway, got presidential medal of freedom that.
this question asking about, although can pretty sure not trying land rocket. term "real time" used pretty defined in software engineering got muddled financial industry. in apollo 11 meant system has hard upper limit on maximum response time external events. rockets need such system, can't late sometimes when adjusting nozzle, being late once produces billion dollar ball of fire. financial industry hijacked mean system that's arbitrarily fast, being late isn't going vaporize machine although makes odds trading loss greater. consider disaster :)
the memory allocator use matters lot, not defined in question. arbitrarily i'll assume program running on demand-paged virtual memory operating system. not ideal environment real time system common enough, true real-time operating systems haven't fared well.
two-phase construction technique used deal initialization failure, exceptions thrown in constructor difficult deal with, destructor not run can cause resource leak if allocate in constructor without otherwise making constructor smart enough deal mishap. alternative later, inside member function, lazily allocating needed.
so worry lazy allocation going hamper responsiveness of system. producing system error 1201.
this not in fact primary concern on demand-paged virtual memory operating system linux or windows. memory allocator on these operating system fast, allocates virtual memory. doesn't cost anything, virtual. true cost comes later, when start use allocated memory. "demand" of demand-paged comes play. addressing array element going produces page fault, forcing operating system map addressed virtual memory page ram. such page faults relatively cheap, called "soft" page faults, if machine isn't otherwise under pressure , must unmap page being used process acquire ram. you'd expect os able grab page , map it, overhead measured in microseconds.
so in effect, if right , don't try initialize entire array when allocate program subjected tens of thousands of tiny needle pricks of overhead. each single 1 small enough not endanger real-time response guarantee. happen regardless of whether allocate memory or late, whether use two-phase construction doesn't matter.
if want guarantee doesn't happen either, or want resilient storm of page faults when initialize entire array, you'll need different approach, need page-lock ram allocation operating system cannot unmap page. invariably requires tinkering os settings, typically doesn't allow process page-lock large amounts of memory. two-phase construction out of door of course.
do keep in mind pretty rare program know how deal allocation failure. behave almost asynchronous exceptions, ready strike @ point in time in part of program. hard reconcile real-time requirement, system has no response real-time event because ran out of memory of course no better 1 that's late. that's still ball of fire ;) in should enough reason not bother two-phase construction, allocate memory @ program initialization time, before start promising real-time response. makes coding program lot simpler, odds failure lower.
a pretty hard requirement software runs real-time characteristics won't have fight other processes acquire operating system resources. dedicating entire machine 1 process expected, not restricted 36864 words of rope memory , 2048 words of ram anymore agc. hardware cheap , plentiful enough these days provide such guarantee.
Comments
Post a Comment