forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_rps.cc
More file actions
1905 lines (1810 loc) · 70.8 KB
/
load_rps.cc
File metadata and controls
1905 lines (1810 loc) · 70.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************
* file load_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* It has the code for loading the persistent store, in JSON
* format. See also http://json.org/ &
* https://github.com/open-source-parsers/jsoncpp/
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright 2019 - 2025 The Reflective Persistent System Team
* team@refpersys.org & http://refpersys.org/
*
* License:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Notice:
* Same code used to be in store_rps.cc file in july 2022.
******************************************************************************/
#include "refpersys.hh"
extern "C" const char rps_load_gitid[];
const char rps_load_gitid[]= RPS_GITID;
extern "C" const char rps_load_date[];
const char rps_load_date[]= __DATE__;
extern "C" const char rps_load_shortgitid[];
const char rps_load_shortgitid[]= RPS_SHORTGITID;
extern "C" char rps_loaded_directory[rps_path_byte_size];
char rps_loaded_directory[rps_path_byte_size];
Json::Value
rps_load_string_to_json(const std::string&str, const char*filnam, int lineno)
{
Json::CharReaderBuilder jsonreaderbuilder;
std::unique_ptr<Json::CharReader> pjsonreader(jsonreaderbuilder.newCharReader());
Json::Value jv;
JSONCPP_STRING errstr;
RPS_ASSERT(pjsonreader);
if (!pjsonreader->parse(str.c_str(), str.c_str() + str.size(), &jv, &errstr))
{
if (filnam != nullptr && lineno > 0)
{
RPS_WARNOUT("JSON parse failure (loading) at " << filnam << ":" << lineno
<< std::endl << str);
}
throw std::runtime_error(std::string("JSON parsing error:") + errstr);
}
return jv;
} // end rps_load_string_to_json
//////////////////////////////////////////////// loader
class Rps_Loader
{
std::string ld_topdir;
double ld_startclock;
/// dlsym and dlopen are not reentrant, so we need a mutex; is is
/// recursive since we might lock it in a nested way
std::recursive_mutex ld_mtx;
/// set of space ids
std::set<Rps_Id> ld_spaceset;
/// set of global roots id
std::set<Rps_Id> ld_globrootsidset;
/// mapping from plugins id to their dlopen-ed handle
std::map<Rps_Id,void*> ld_pluginsmap;
/// map of loaded objects
std::map<Rps_Id,Rps_ObjectRef> ld_mapobjects;
/// double ended queue of todo chunks in second pass
struct todo_st
{
double todo_addtime;
std::function<void(Rps_Loader*)> todo_fun;
todo_st() : todo_addtime(0.0), todo_fun() {};
todo_st(double d, std::function<void(Rps_Loader*)> f)
: todo_addtime(d), todo_fun(f) {};
};
std::deque<struct todo_st> ld_todoque;
unsigned ld_todocount;
static constexpr unsigned ld_maxtodo = 1<<20;
/// dictionary of payload loaders - used as a cache to avoid most dlsym-s
std::map<std::string,rpsldpysig_t*> ld_payloadercache;
bool is_object_starting_line(Rps_Id spacid, unsigned lineno, const std::string&linbuf, Rps_Id*pobid);
Rps_ObjectRef fetch_one_constant_at(const char*oid,int lin);
void parse_json_buffer_second_pass (Rps_Id spacid, unsigned lineno,
Rps_Id objid, const std::string& objbuf, unsigned count);
public:
Rps_Loader(const std::string&topdir);
~Rps_Loader();
void parse_manifest_file(void);
void parse_user_manifest(const std::string&path);
void first_pass_space(Rps_Id spacid);
void initialize_root_objects(void);
void initialize_constant_objects(void);
void second_pass_space(Rps_Id spacid);
std::string string_of_loaded_file(const std::string& relpath);
std::string space_file_path(Rps_Id spacid);
std::string load_real_path(const std::string& path);
Rps_ObjectRef find_object_by_oid(Rps_Id oid)
{
auto it = ld_mapobjects.find(oid);
if (it != ld_mapobjects.end())
return it->second;
return Rps_ObjectRef(nullptr);
};
void load_all_state_files(void);
void add_todo(const std::function<void(Rps_Loader*)>& todofun);
void set_primitive_type_size_and_align(Rps_ObjectRef primtypob,
unsigned sizeby, unsigned alignby);
// run some todo functions, return the number of remaining ones
int run_some_todo_functions(void);
void load_install_roots(void);
unsigned nb_loaded_objects(void) const
{
return ld_mapobjects.size();
};
}; // end class Rps_Loader
Rps_Loader::Rps_Loader(const std::string&topdir) :
ld_topdir(topdir),
ld_startclock(rps_wallclock_real_time()),
ld_mtx(),
ld_spaceset(),
ld_globrootsidset(),
ld_pluginsmap(),
ld_mapobjects(),
ld_todoque(),
ld_todocount(0),
ld_payloadercache()
{
RPS_DEBUG_LOG(LOAD, "Rps_Loader constr topdir=" << topdir
<< " this@" << (void*)this
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_Loader constr"));
} // end Rps_Loader::Rps_Loader
Rps_Loader::~Rps_Loader()
{
RPS_DEBUG_LOG(LOAD, "Rps_Loader destr topdir=" << ld_topdir
<< " this@" << (void*)this
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_Loader constr"));
} // end Rps_Loader::~Rps_Loader
std::string
Rps_Loader::load_real_path(const std::string& path)
{
if (path.size() > 2 && path[0] == '/')
{
if (access(path.c_str(), R_OK))
{
int e = errno;
RPS_WARN("loader cannot access %s - %s",
path.c_str(), strerror(e));
throw std::runtime_error(path + ":" + strerror(e));
}
char*rp = realpath(path.c_str(), nullptr);
if (!rp)
throw std::runtime_error(std::string("realpath failed:") + path);
std::string restr (rp);
free (rp), rp = nullptr;
return restr;
}
std::string candipath;// candidate path
candipath = ld_topdir + "/" + path;
if (!access(candipath.c_str(), R_OK))
{
char*rp = realpath(candipath.c_str(), nullptr);
if (!rp)
throw std::runtime_error(std::string("realpath failed:") + candipath);
std::string restr (rp);
free (rp), rp = nullptr;
RPS_DEBUG_LOG(LOAD, "load_real_path path=" << path << " -> " << restr);
return restr;
}
throw std::runtime_error(std::string("cannot file load real path for ") + path);
} // end Rps_Loader::load_real_path
std::string
Rps_Loader::space_file_path(Rps_Id spacid)
{
if (!spacid.valid())
throw std::runtime_error("Rps_Loader::space_file_path invalid spacid");
return std::string{"persistore/sp"} + spacid.to_string() + "-rps.json";
} // end Rps_Loader::space_file_path
bool
Rps_Loader::is_object_starting_line(Rps_Id spacid, unsigned lineno, const std::string&linbuf, Rps_Id*pobid)
{
const char*reason = nullptr;
const char*oidstart = nullptr;
const char*end = nullptr;
const char*linestart = nullptr;
char reasonbuf[64];
Rps_Id oid;
bool ok=false;
char c = 0;
int cix= -1;
memset (reasonbuf, 0, sizeof(reasonbuf));
size_t linelen = linbuf.size();
if (pobid)
*pobid = Rps_Id(nullptr);
if (linelen < 8 || linbuf[0] != '/' || linbuf[1] != '/'
|| linbuf[2] != '+'
|| linbuf[3] != 'o'
|| linbuf[4] != 'b'
|| linbuf[5] != '_')
return false;
if (linelen < strlen ("//+ob") + Rps_Id::nbchars)
{
reason = "too short";
goto bad;
}
linestart = linbuf.c_str();
oidstart = linestart + strlen("//+ob");
char oidbuf[Rps_Id::nbchars+8];
memset (oidbuf, 0, sizeof(oidbuf));
oidbuf[0] = '_';
for (cix=1; cix<(int)Rps_Id::nbchars; cix++)
{
c = oidstart[cix];
switch (c)
{
case '0' ... '9':
case 'a' ... 'z':
case 'A' ... 'Z':
oidbuf[cix] = c;
continue;
default:
memset (reasonbuf, 0, sizeof(reasonbuf));
if (c > ' ' && c < 127)
snprintf (reasonbuf, sizeof(reasonbuf)-1,
"bad character %c at column %d",
c, (int)(oidstart+cix-linestart));
else
snprintf (reasonbuf, sizeof(reasonbuf)-1,
"bad character code %d (\\%#02x) at colum %d",
(int)c, (int)c, (int)( oidstart+cix-linestart));
reason = reasonbuf;
goto bad;
} // end case c
};
{
Rps_Id tempoid(oidbuf, &end, &ok);
if (!end || (*end && !isspace(*end) && *end != ':'))
{
reason= "too long";
goto bad;
}
oid = tempoid;
};
if (!ok)
{
reason= "bad oid";
goto bad;
}
if (!oid.valid())
{
reason= "invalid oid";
goto bad;
}
if (pobid)
*pobid = oid;
return true;
bad:
if (!reason)
reason="???";
RPS_WARNOUT("bad object starting line in space " << spacid << " line#" << lineno
<< " - " << reason
<< ":" << std::endl
<< linbuf);
return false;
} // end Rps_Loader::is_object_starting_line
void
Rps_Loader::first_pass_space(Rps_Id spacid)
{
auto spacepath = load_real_path(space_file_path(spacid));
std::ifstream ins(spacepath);
std::string prologstr;
int obcnt = 0;
int expectedcnt = 0;
unsigned lincnt = 0;
RPS_DEBUG_LOG(LOAD, "first_pass_space start spacepath=" << spacepath);
for (std::string linbuf; std::getline(ins, linbuf); )
{
lincnt++;
if (u8_check(reinterpret_cast<const uint8_t*> (linbuf.c_str()),
linbuf.size()))
{
RPS_WARN("non UTF8 line#%d in %s:\n%s",
lincnt, spacepath.c_str(), linbuf.c_str());
char errbuf[40];
snprintf(errbuf, sizeof(errbuf), "non UTF8 line#%d", lincnt);
throw std::runtime_error(std::string(errbuf) + " in " + spacepath);
}
if (RPS_UNLIKELY(obcnt == 0))
{
prologstr += linbuf;
prologstr += '\n';
}
Rps_Id curobjid;
if (is_object_starting_line(spacid, lincnt, linbuf, &curobjid))
{
RPS_DEBUG_LOG(LOAD, "firstpass got ob spacid:" << spacid
<< " linbuf: " << linbuf
<< " lincnt#" << lincnt
<< " curobjid:" << curobjid
<< " count:" << (obcnt+1));
if (RPS_UNLIKELY(obcnt == 0))
{
Json::Value prologjson;
try
{
prologjson = rps_load_string_to_json(prologstr);
if (prologjson.type() != Json::objectValue)
RPS_FATAL("Rps_Loader::first_pass_space %s line#%d bad Json type #%d",
spacepath.c_str(), (int)lincnt, (int)prologjson.type());
}
catch (std::exception& exc)
{
RPS_FATALOUT("Rps_Loader::first_pass_space " << " spacepath:" << spacepath
<< " line#" << lincnt
<< " failed to parse: " << exc.what());
};
Json::Value formatjson = prologjson["format"];
if (formatjson.type() !=Json::stringValue)
RPS_FATALOUT("space file " << spacepath
<< " with bad format type#" << (int)formatjson.type());
if (formatjson.asString() != RPS_MANIFEST_FORMAT
&& formatjson.asString() != RPS_PREVIOUS_MANIFEST_FORMAT)
RPS_FATALOUT("space file " << spacepath
<< "should have format: "
<< RPS_MANIFEST_FORMAT
<< " or " << RPS_PREVIOUS_MANIFEST_FORMAT
<< " but got "
<< formatjson);
if (prologjson["spaceid"].asString() != spacid.to_string())
RPS_FATAL("spacefile %s should have spaceid: '%s' but got '%s'",
spacepath.c_str (), spacid.to_string().c_str(),
prologjson["spaceid"].asString().c_str());
int majv = prologjson["rpsmajorversion"].asInt();
int minv = prologjson["rpsminorversion"].asInt();
if (majv != rps_get_major_version()
|| minv != rps_get_minor_version())
RPS_WARNOUT("space file " << spacepath
<< " was dumped by RefPerSys " << majv << "." << minv
<< " but is loaded by RefPerSys " << rps_get_major_version()
<< "." << rps_get_minor_version());
Json::Value nbobjectsjson = prologjson["nbobjects"];
expectedcnt =nbobjectsjson.asInt();
}
Rps_ObjectRef obref(Rps_ObjectZone::make_loaded(curobjid, this));
if (ld_mapobjects.find(curobjid) != ld_mapobjects.end())
{
RPS_WARN("duplicate object of oid %s in line#%d in %s",
curobjid.to_string().c_str(), lincnt, spacepath.c_str());
throw std::runtime_error(std::string("duplicate objid "
+ curobjid.to_string() + " in " + spacepath));
}
ld_mapobjects.insert({curobjid,obref});
obcnt++;
}
}
if (obcnt != expectedcnt)
{
RPS_WARN("got %d objects in loaded space %s but expected %d of them",
obcnt, spacepath.c_str(), expectedcnt);
throw std::runtime_error(std::string("unexpected object count in ")
+ spacepath);
}
RPS_DEBUG_LOG(LOAD, "first_pass_space end spacepath=" << spacepath << " obcnt="<< obcnt << std::endl
<< "… read " << obcnt
<< " objects while loading first pass of " << spacepath);
} // end Rps_Loader::first_pass_space
void
Rps_Loader::add_todo(const std::function<void(Rps_Loader*)>& todofun)
{
std::lock_guard<std::recursive_mutex> gu(ld_mtx);
ld_todoque.push_back(todo_st{rps_elapsed_real_time(),todofun});
} // end Rps_Loader::add_todo
// return the number of remaining todo functions
int
Rps_Loader::run_some_todo_functions(void)
{
double startim = rps_elapsed_real_time();
constexpr int dosteps = 24;
constexpr double doelaps = 0.05;
int count=0;
/// run at least the front todo entry
{
todo_st td;
{
std::lock_guard<std::recursive_mutex> gu(ld_mtx);
bool emptyq = ld_todoque.empty();
if (emptyq)
return 0;
td = ld_todoque.front();
ld_todoque.pop_front();
if (ld_todocount++ > ld_maxtodo)
RPS_FATALOUT("too many " << ld_todocount << " loader todo functions");
}
td.todo_fun(this);
count++;
}
/// run more entries provided they have been added before start
while (count < dosteps && rps_elapsed_real_time() - startim < doelaps)
{
todo_st td;
{
std::lock_guard<std::recursive_mutex> gu(ld_mtx);
bool emptyq = ld_todoque.empty();
if (emptyq)
return 0;
td = ld_todoque.front();
if (td.todo_addtime > startim)
return ld_todoque.size();
ld_todoque.pop_front();
if (ld_todocount++ > ld_maxtodo)
RPS_FATALOUT("too many " << ld_todocount << " loader todo functions");
}
td.todo_fun(this);
count++;
}
/// finally
{
std::lock_guard<std::recursive_mutex> gu(ld_mtx);
return ld_todoque.size();
}
} // end of Rps_Loader::run_some_todo_functions
void
rps_load_add_todo(Rps_Loader*ld,const std::function<void(Rps_Loader*)>& todofun)
{
RPS_ASSERT(ld != nullptr);
RPS_ASSERT(todofun);
ld->add_todo(todofun);
} // end rps_load_add_todo
void
Rps_Loader::initialize_root_objects(void)
{
std::lock_guard<std::recursive_mutex> gu(ld_mtx);
#define RPS_INSTALL_ROOT_OB(Oid) do { \
if (!RPS_ROOT_OB(Oid)) \
RPS_ROOT_OB(Oid) \
= find_object_by_oid(Rps_Id(#Oid)); \
RPS_ASSERT(RPS_ROOT_OB(Oid)); \
} while(0);
#include "generated/rps-roots.hh"
} // end Rps_Loader::initialize_root_objects
void
Rps_Loader::initialize_constant_objects(void)
{
std::lock_guard<std::recursive_mutex> gu(ld_mtx);
#define RPS_INSTALL_CONSTANT_OB(Oid) \
rpskob##Oid = fetch_one_constant_at(#Oid, __LINE__);
#include "generated/rps-constants.hh"
} // end of Rps_Loader::initialize_constant_objects
Rps_ObjectRef
Rps_Loader::fetch_one_constant_at(const char*oidstr, int lin)
{
const char*end = nullptr;
bool ok = false;
Rps_Id id(oidstr, &end, &ok);
RPS_ASSERT(end && *end==(char)0 && ok);
auto it = ld_mapobjects.find(id);
if (it == ld_mapobjects.end())
{
RPS_WARNOUT("failed to fetch constant " << oidstr
<< " at line " << lin << " of generated/rps-constants.hh");
return nullptr;
}
else
return it->second;
} // end Rps_Loader::fetch_one_constant_at
////////////////
void
Rps_Loader::parse_json_buffer_second_pass (Rps_Id spacid, unsigned lineno,
Rps_Id objid, const std::string& objbuf, unsigned count)
{
RPS_DEBUG_LOG(LOAD, "parse_json_buffer_second_pass start spacid=" << spacid << " #" << count
<< " lineno=" <<lineno
<< " objid=" <<objid
<< " objbuf:\n" << objbuf);
Json::Value objjson;
try
{
objjson = rps_load_string_to_json(objbuf);
if (objjson.type() != Json::objectValue)
RPS_FATALOUT("parse_json_buffer_second_pass spacid=" << spacid
<< " lineno:" << lineno
<< " objid:" << objid
<< " bad objbuf:" << std::endl
<< objbuf
<< std::endl << "… and objjson:" << objjson);
}
catch (std::exception& exc)
{
RPS_FATALOUT("parse_json_buffer_second_pass spacid=" << spacid
<< " lineno:" << lineno
<< " objid:" << objid
<< " parse failure "
<< exc.what()
<< "… with objbuf:" << std::endl
<< objbuf
<< std::endl << "… and objjson:" << objjson);
};
//// now load the various JSON members
Json::Value oidjson = objjson["oid"];
if (oidjson.asString() != objid.to_string())
RPS_FATALOUT("parse_json_buffer_second_pass spacid=" << spacid
<< " lineno:" << lineno
<< " objid:" << objid
<< " unexpected");
auto obz = Rps_ObjectZone::find(objid);
if (!obz)
RPS_FATALOUT("parse_json_buffer_second_pass spacid=" << spacid
<< " lineno:" << lineno
<< " unknown objid:" << objid);
auto obzspace = Rps_ObjectZone::find(spacid);
obz->loader_set_class (this, Rps_ObjectRef(objjson["class"], this));
RPS_ASSERT (obzspace);
obz->loader_set_space (this, obzspace);
double mtim = objjson["mtime"].asDouble();
if (mtim > this->ld_startclock + 300.0)
{
double cormtim = this->ld_startclock + 300.0;
RPS_WARNOUT("parse_json_buffer_second_pass mtime of object " << objid
<< " is too far in the future in spacid=" << spacid
<< " lineno:" << lineno
<< " changed from " << mtim << " to " << cormtim);
mtim = cormtim;
}
obz->loader_set_mtime (this,mtim);
if (objjson.isMember("comps"))
{
auto compjson = objjson["comps"];
int siz= 0;
if (compjson.isArray())
{
siz = compjson.size();
RPS_DEBUG_LOG(LOAD, "parse_json_buffer_second_pass obz=" << obz << " comps#" << siz);
obz->loader_reserve_comps(this, (unsigned)siz);
for (int ix=0; ix<(int)siz; ix++)
{
auto valcomp = Rps_Value(compjson[ix], this);
obz->loader_add_comp(this, valcomp);
}
}
else
RPS_WARNOUT("parse_json_buffer_second_pass spacid=" << spacid
<< " lineno:" << lineno
<< " objid:" << objid
<< " bad compjson:" << compjson);
}
if (objjson.isMember("attrs"))
{
auto attrjson = objjson["attrs"];
int siz= 0;
if (attrjson.isArray())
{
siz = attrjson.size();
RPS_DEBUG_LOG(LOAD, "parse_json_buffer_second_pass obz=" << obz << " attrs#" << siz);
for (int ix=0; ix<(int)siz; ix++)
{
auto entjson = attrjson[ix];
int entsiz= 0;
if (entjson.isObject()
&& (entsiz=entjson.size()) >= 2
&& entjson.isMember("at")
&& entjson.isMember("va")
)
{
auto atobr = Rps_ObjectRef(entjson["at"], this);
RPS_ASSERT(atobr);
auto atval = Rps_Value(entjson["va"], this);
RPS_ASSERT(atval);
obz->loader_put_attr(this, atobr, atval);
}
}
}
else RPS_WARNOUT("parse_json_buffer_second_pass spacid=" << spacid
<< " lineno:" << lineno
<< " objid:" << objid
<< " bad attrjson:" << attrjson);
}
if (objjson.isMember("magicattr"))
{
RPS_DEBUG_LOG(LOAD, "parse_json_buffer_second_pass magicattr objid=" << objid);
std::lock_guard<std::recursive_mutex> gu(ld_mtx);
char getfunambuf[sizeof(RPS_GETTERFUN_PREFIX)+8+Rps_Id::nbchars];
memset(getfunambuf, 0, sizeof(getfunambuf));
char obidbuf[32];
memset (obidbuf, 0, sizeof(obidbuf));
objid.to_cbuf24(obidbuf);
strcpy(getfunambuf, RPS_GETTERFUN_PREFIX);
strcat(getfunambuf+strlen(RPS_GETTERFUN_PREFIX), obidbuf);
RPS_ASSERT(strlen(getfunambuf)<sizeof(getfunambuf)-4);
void*funad = dlsym(rps_proghdl, getfunambuf);
if (!funad)
RPS_FATALOUT("cannot dlsym " << getfunambuf << " for magic attribute getter of objid:" << objid
<< " lineno:" << lineno << ", spacid:" << spacid
<< ":: " << dlerror());
obz->loader_put_magicattrgetter(this, reinterpret_cast<rps_magicgetterfun_t*>(funad));
}; // end with "magicattr" JSON member
if (objjson.isMember("applying"))
{
RPS_DEBUG_LOG(LOAD, "parse_json_buffer_second_pass applying objid=" << objid);
std::lock_guard<std::recursive_mutex> gu(ld_mtx);
char appfunambuf[sizeof(RPS_APPLYINGFUN_PREFIX)+8+Rps_Id::nbchars];
memset(appfunambuf, 0, sizeof(appfunambuf));
char obidbuf[32];
memset (obidbuf, 0, sizeof(obidbuf));
objid.to_cbuf24(obidbuf);
strcpy(appfunambuf, RPS_APPLYINGFUN_PREFIX);
strcat(appfunambuf+strlen(RPS_APPLYINGFUN_PREFIX), obidbuf);
RPS_ASSERT(strlen(appfunambuf)<sizeof(appfunambuf)-4);
void*funad = dlsym(rps_proghdl, appfunambuf);
if (!funad)
RPS_FATALOUT("cannot dlsym " << appfunambuf << " for applying function of objid:" << objid
<< " lineno:" << lineno << ", spacid:" << spacid
<< ":: " << dlerror());
obz->loader_put_applyingfunction(this, reinterpret_cast<rps_applyingfun_t*>(funad));
}; // end with "applying" JSON member
if (objjson.isMember("payload"))
{
rpsldpysig_t*pldfun = nullptr;
auto paylstr = objjson["payload"].asString();
{
std::lock_guard<std::recursive_mutex> gu(ld_mtx);
auto ldit = ld_payloadercache.find(paylstr);
if (RPS_UNLIKELY(ldit == ld_payloadercache.end()))
{
char firstc = paylstr.at(0);
if (isalpha(firstc))
{
std::string symstr = std::string(RPS_PAYLOADING_PREFIX) + paylstr;
void* symad = dlsym(rps_proghdl, symstr.c_str());
if (!symad)
RPS_FATALOUT("cannot dlsym " << symstr << " for payload of objid:" << objid
<< " lineno:" << lineno << ", spacid:" << spacid
<< ":: " << dlerror());
pldfun = (rpsldpysig_t*)symad;
ld_payloadercache.insert({paylstr, pldfun});
}
else if (firstc=='_')
{
auto pyid = Rps_Id(paylstr);
if (!pyid.valid())
RPS_FATALOUT("Rps_Loader::parse_json_buffer_second_pass spacid:" << spacid
<< " lineno:" << lineno
<< " objid:" << objid
<< " invalid id payload:" << paylstr);
}
else
RPS_FATALOUT("Rps_Loader::parse_json_buffer_second_pass spacid:" << spacid
<< " lineno:" << lineno
<< " objid:" << objid
<< " invalid payload:" << paylstr);
}
else
pldfun = ldit->second;
};
if (pldfun)
{
(*pldfun)(obz,this,objjson,spacid,lineno);
}
else
{
RPS_FATALOUT("Rps_Loader::parse_json_buffer_second_pass in spacid=" << spacid
<< " lineno:" << lineno
<< " objid:" << objid
<< " payload: " << paylstr
<< " without loading function"
<< std::endl);
}
}; //// end handling of "payload" JSON member
if (obz->is_instance_of(RPS_ROOT_OB(_3O1QUNKZ4bU02amQus) //∈rps_routine
))
{
std::lock_guard<std::recursive_mutex> gu(ld_mtx);
char appfunambuf[sizeof(RPS_APPLYINGFUN_PREFIX)+8+Rps_Id::nbchars];
memset(appfunambuf, 0, sizeof(appfunambuf));
char obidbuf[32];
memset (obidbuf, 0, sizeof(obidbuf));
obz->oid().to_cbuf24(obidbuf);
strcpy(appfunambuf, RPS_APPLYINGFUN_PREFIX);
strcat(appfunambuf+strlen(RPS_APPLYINGFUN_PREFIX), obidbuf);
RPS_ASSERT(strlen(appfunambuf)<sizeof(appfunambuf)-4);
void*funad = dlsym(rps_proghdl, appfunambuf);
if (!funad)
RPS_WARNOUT("cannot dlsym " << appfunambuf << " for applying function of objid:" << objid
<< Rps_ObjectRef(obz)
<< " lineno:" << lineno << ", spacid:" << spacid
<< ":: " << dlerror());
else
obz->loader_put_applyingfunction(this, reinterpret_cast<rps_applyingfun_t*>(funad));
};
if (objjson.isMember("loadrout"))
{
auto loadroutstr = objjson["loadrout"].asString();
std::lock_guard<std::recursive_mutex> gu(ld_mtx);
if (loadroutstr.empty())
RPS_WARNOUT("invalid loadrout for loading routine function of objid:" << objid
<< Rps_ObjectRef(obz)
<< " lineno:" << lineno << ", spacid:" << spacid
<< std::endl << objjson);
else
{
void*ldroutad = dlsym(rps_proghdl, loadroutstr.c_str());
if (!ldroutad)
RPS_WARNOUT("cannot dlsym " << loadroutstr
<< " for loading routine function of objid:" << objid
<< Rps_ObjectRef(obz)
<< " lineno:" << lineno << ", spacid:" << spacid
<< ":: " << dlerror());
rpsldpysig_t*ldrout = (rpsldpysig_t*)ldroutad;
(*ldrout)(obz, this, objjson, spacid, lineno);
};
}; // end if has "loadrout" member
RPS_DEBUG_LOG(LOAD, "parse_json_buffer_second_pass end objid=" << objid << " #" << count
<< std::endl);
} // end of Rps_Loader::parse_json_buffer_second_pass
////////////////////////////////////////////////////////////////
void
Rps_Loader::second_pass_space(Rps_Id spacid)
{
RPS_DEBUG_LOG(LOAD, "Rps_Loader::second_pass_space start spacid:" << spacid
<< std::endl << RPS_FULL_BACKTRACE_HERE(0, "RpsLoader::second_pass_space"));
auto spacepath = load_real_path(space_file_path(spacid));
std::ifstream ins(spacepath);
unsigned lincnt = 0;
unsigned obcnt = 0;
Rps_Id prevoid;
unsigned prevlin=0;
std::string objbuf;
for (std::string linbuf; std::getline(ins, linbuf); )
{
lincnt++;
if (linbuf.size() > 0 && linbuf[0] == '#')
continue;
Rps_Id curobjid;
if (is_object_starting_line(spacid,lincnt,linbuf,&curobjid))
{
obcnt++;
RPS_DEBUG_LOG(LOAD, "secondpass lincnt="<< lincnt
<< " curobjid=" << curobjid
<< " obcnt=" << obcnt);
if (objbuf.size() > 0 && prevoid && prevlin>0)
{
try
{
parse_json_buffer_second_pass(spacid, prevlin, prevoid, objbuf, obcnt);
}
catch (const std::exception& exc)
{
RPS_FATALOUT("failed second pass in space " << spacid
<< " prevoid:" << prevoid
<< " line#" << prevlin
<< std::endl
<< "… got exception of type "
<< typeid(exc).name()
<< ":"
<< exc.what());
};
prevoid = Rps_Id(nullptr);
}
objbuf.clear();
objbuf = linbuf + '\n';
prevoid = curobjid;
prevlin = lincnt;
}
else if (objbuf.size() > 0)
{
objbuf += linbuf;
objbuf += '\n';
}
} // end for getline
if (objbuf.size() > 0 && prevoid && prevlin>0)
{
try
{
parse_json_buffer_second_pass(spacid, prevlin, prevoid, objbuf, obcnt);
}
catch (const std::exception& exc)
{
RPS_FATALOUT("failed second pass in " << spacid
<< " prevoid:" << prevoid
<< " line#" << prevlin
<< std::endl
<< "… got exception of type "
<< typeid(exc).name()
<< ":"
<< exc.what());
};
prevoid = Rps_Id(nullptr);
};
RPS_DEBUG_LOG(LOAD, "Rps_Loader::second_pass_space end spacid:" << spacid);
} // end of Rps_Loader::second_pass_space
void
Rps_Loader::load_all_state_files(void)
{
const char*thisprog = (rps_progexe[0]?rps_progexe
:rps_progname?rps_progname:"*RefPerSys*");
RPS_ASSERT(thisprog != nullptr);
RPS_DEBUG_LOG(LOAD, "Rps_Loader::load_all_state_files start this@" << (void*)this
<< std::endl << RPS_FULL_BACKTRACE_HERE(0, "RpsLoader::load_all_state_files"));
int spacecnt1 = 0, spacecnt2 = 0;
Rps_Id initialspaceid("_8J6vNYtP5E800eCr5q"); //"initial_space"∈space
first_pass_space(initialspaceid);
spacecnt1++;
initialize_root_objects();
for (Rps_Id spacid: ld_spaceset)
{
if (spacid != initialspaceid)
{
char spacbuf[32];
memset (spacbuf, 0, sizeof(spacbuf));
spacid.to_cbuf24(spacbuf);
first_pass_space(spacid);
spacecnt1++;
RPS_INFORM("%s loaded extra space#%d %s", thisprog, spacecnt1, spacbuf);
}
}
RPS_INFORM("%s loaded %d space files in first pass",
thisprog, spacecnt1);
initialize_constant_objects();
/// conceptually, the second pass might be done in parallel
/// (multi-threaded, with different threads working on different
/// spaces), but this require more clever locking and
/// synchronization, so might be done after reaching our milestone#2
for (Rps_Id spacid: ld_spaceset)
{
run_some_todo_functions();
second_pass_space(spacid);
spacecnt2++;
usleep(30);
}
RPS_INFORM("%s loaded %d space files in second pass",
thisprog, spacecnt2);
rps_load_add_todo(this, rps_initialize_carburetta_after_load);
while (run_some_todo_functions()>0)
{
// we sleep a tiny bit, so elapsed time is growing...
usleep(20);
};
RPS_DEBUG_LOG(LOAD, "Rps_Loader::load_all_state_files end this@"
<< (void*)this);
RPS_INFORM("%s loaded %d space files in first pass,\n"
" %d space files in second passes,\n"
" %ld objects from directory %s,\n"
" in %.2f real sec (pid %ld on host %s git %s)",
thisprog,
spacecnt1, spacecnt2, (long)ld_mapobjects.size(),
ld_topdir.c_str(), rps_wallclock_real_time() - ld_startclock,
(long)getpid(), rps_hostname(), rps_shortgitid);
RPS_DEBUG_LOG(LOAD, "Rps_Loader::load_all_state_files end this@"
<< (void*)this << std::endl
<< RPS_FULL_BACKTRACE_HERE(0, "RpsLoader::load_all_state_files"));
} // end Rps_Loader::load_all_state_files
std::string
Rps_Loader::string_of_loaded_file(const std::string&relpath)
{
RPS_DEBUG_LOG(LOAD, "Rps_Loader::string_of_loaded_file start this@" << (void*)this
<< " relpath=" << relpath
<< std::endl << RPS_FULL_BACKTRACE_HERE(0, "RpsLoader::string_of_loaded_file"));
constexpr size_t maxfilen = 1024*1024; // a megabyte
std::string fullpath = load_real_path(relpath);
std::string res;
int lincnt=0;
std::ifstream inp(fullpath);
for (std::string linbuf; std::getline(inp, linbuf); )
{
lincnt++;
if (u8_check(reinterpret_cast<const uint8_t*> (linbuf.c_str()),
linbuf.size()))
{
RPS_WARN("non UTF8 line#%d in %s:\n%s",
lincnt, fullpath.c_str(), linbuf.c_str());
char errbuf[40];
snprintf(errbuf, sizeof(errbuf), "non UTF8 line#%d", lincnt);
throw std::runtime_error(std::string(errbuf) + " in " + fullpath);
}
res += linbuf;
res += '\n';
if (RPS_UNLIKELY(res.size() > maxfilen))
RPS_FATAL("too big file %zd of path %s", res.size(), fullpath.c_str());
}
RPS_DEBUG_LOG(LOAD, "Rps_Loader::string_of_loaded_file end this@" << (void*)this);
return res;
} // end Rps_Loader::string_of_loaded_file
Rps_Value::Rps_Value(const Json::Value &jv, Rps_Loader*ld)
: Rps_Value(nullptr)
{
RPS_ASSERT(ld != nullptr);
std::int64_t i=0;
std::string str = "";
std::size_t siz=0;
std::size_t subsiz=0;
Json::Value jcomp;
Json::Value jvtype;
if (jv.isInt64())
{
i = jv.asInt64();
*this = Rps_Value(i, Rps_IntTag{});
return;
}
else if (jv.isDouble())
{
double d = jv.asDouble();
RPS_ASSERT(!std::isnan(d));
*this = Rps_Value(d, Rps_DoubleTag{});
return;
}
else if (jv.isNull())
{
*this = Rps_Value(nullptr);
return;
}
else if (jv.isString())
{
str = jv.asString();
if (str.size() == Rps_Id::nbchars && str[0] == '_' && isalnum(str[1])
&& std::all_of(str.begin()+1, str.end(),
[](char c)
{
return strchr(Rps_Id::b62digits, c) != nullptr;
}))
{
*this = Rps_ObjectValue(Rps_ObjectRef(jv, ld));
RPS_ASSERT(*this);
return;
}
*this = Rps_StringValue(str);
return;
}
else if (jv.isObject() && jv.size()==1 && jv.isMember("string")
&& (jcomp=jv["string"]).isString())
{
str=jcomp.asString();
*this = Rps_StringValue(str);
return;