/
DatabaseSnapshot.cpp
1217 lines (1005 loc) · 32.6 KB
/
DatabaseSnapshot.cpp
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
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Dmitry Yemanov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2006 Dmitry Yemanov <dimitr@users.sf.net>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include "firebird.h"
#include "ids.h"
#include "../common/classes/auto.h"
#include "../common/classes/locks.h"
#include "../jrd/gdsassert.h"
#include "../jrd/jrd.h"
#include "../jrd/cch.h"
#include "../jrd/ini.h"
#include "../jrd/nbak.h"
#include "../jrd/os/guid.h"
#include "../jrd/os/pio.h"
#include "../jrd/req.h"
#include "../jrd/tra.h"
#include "../jrd/blb_proto.h"
#include "../jrd/isc_proto.h"
#include "../jrd/isc_f_proto.h"
#include "../jrd/isc_s_proto.h"
#include "../jrd/lck_proto.h"
#include "../jrd/met_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/os/pio_proto.h"
#include "../jrd/pag_proto.h"
#include "../jrd/thread_proto.h"
#include "../jrd/Relation.h"
#include "../jrd/RecordBuffer.h"
#include "../jrd/DatabaseSnapshot.h"
#include "../jrd/Function.h"
#include "../common/utils_proto.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef UNIX
#include <signal.h>
#endif
#ifdef WIN_NT
#include <process.h>
#include <windows.h>
#endif
using namespace Firebird;
using namespace Jrd;
// SharedData class
DatabaseSnapshot::SharedData::SharedData(const Database* dbb)
: process_id(getpid()), local_id(dbb->dbb_monitoring_id)
{
string name;
name.printf(MONITOR_FILE, dbb->getUniqueFileId().c_str());
Arg::StatusVector statusVector;
mapFile(statusVector, name.c_str(), DEFAULT_SIZE);
if (!sh_mem_header)
{
iscLogStatus("Cannot initialize the shared memory region", statusVector.value());
status_exception::raise(statusVector);
}
fb_assert(sh_mem_header->mhb_version == MONITOR_VERSION);
}
DatabaseSnapshot::SharedData::~SharedData()
{
{ // scope
DumpGuard guard(this);
cleanup();
if (sh_mem_header->used == sizeof(Header))
removeMapFile();
}
Arg::StatusVector statusVector;
unmapFile(statusVector);
}
void DatabaseSnapshot::SharedData::acquire()
{
mutexLock();
if (sh_mem_header->allocated > sh_mem_length_mapped)
{
#ifdef HAVE_OBJECT_MAP
Arg::StatusVector statusVector;
remapFile(statusVector, sh_mem_header->allocated, false);
if (!remapFile(statusVector, sh_mem_header->allocated, false))
{
status_exception::raise(statusVector);
}
#else
status_exception::raise(Arg::Gds(isc_montabexh));
#endif
}
}
void DatabaseSnapshot::SharedData::release()
{
mutexUnlock();
}
UCHAR* DatabaseSnapshot::SharedData::read(MemoryPool& pool, ULONG& resultSize)
{
ULONG self_dbb_offset = 0;
// Garbage collect elements belonging to dead processes.
// This is done in two passes. First, we compact the data
// and calculate the total size of the resulting data.
// Second, we create a resulting buffer of the necessary size
// and copy the data there, starting with our own dbb.
// First pass
for (ULONG offset = alignOffset(sizeof(Header)); offset < sh_mem_header->used;)
{
UCHAR* const ptr = (UCHAR*) sh_mem_header + offset;
const Element* const element = (Element*) ptr;
const ULONG length = alignOffset(sizeof(Element) + element->length);
if (element->processId == process_id && element->localId == local_id)
{
self_dbb_offset = offset;
}
if (ISC_check_process_existence(element->processId))
{
resultSize += element->length;
offset += length;
}
else
{
fb_assert(sh_mem_header->used >= offset + length);
memmove(ptr, ptr + length, sh_mem_header->used - offset - length);
sh_mem_header->used -= length;
}
}
// Second pass
UCHAR* const buffer = FB_NEW(pool) UCHAR[resultSize];
UCHAR* bufferPtr(buffer);
fb_assert(self_dbb_offset);
UCHAR* const ptr = (UCHAR*) sh_mem_header + self_dbb_offset;
const Element* const element = (Element*) ptr;
memcpy(bufferPtr, ptr + sizeof(Element), element->length);
bufferPtr += element->length;
for (ULONG offset = alignOffset(sizeof(Header)); offset < sh_mem_header->used;)
{
UCHAR* const ptr = (UCHAR*) sh_mem_header + offset;
const Element* const element = (Element*) ptr;
const ULONG length = alignOffset(sizeof(Element) + element->length);
if (offset != self_dbb_offset)
{
memcpy(bufferPtr, ptr + sizeof(Element), element->length);
bufferPtr += element->length;
}
offset += length;
}
fb_assert(buffer + resultSize == bufferPtr);
return buffer;
}
ULONG DatabaseSnapshot::SharedData::setup()
{
ensureSpace(sizeof(Element));
// Put an up-to-date element at the tail
const ULONG offset = sh_mem_header->used;
UCHAR* const ptr = (UCHAR*) sh_mem_header + offset;
Element* const element = (Element*) ptr;
element->processId = process_id;
element->localId = local_id;
element->length = 0;
sh_mem_header->used += alignOffset(sizeof(Element));
return offset;
}
void DatabaseSnapshot::SharedData::write(ULONG offset, ULONG length, const void* buffer)
{
ensureSpace(length);
// Put an up-to-date element at the tail
UCHAR* const ptr = (UCHAR*) sh_mem_header + offset;
Element* const element = (Element*) ptr;
memcpy(ptr + sizeof(Element) + element->length, buffer, length);
ULONG previous = alignOffset(sizeof(Element) + element->length);
element->length += length;
ULONG current = alignOffset(sizeof(Element) + element->length);
sh_mem_header->used += (current - previous);
}
void DatabaseSnapshot::SharedData::cleanup()
{
// Remove information about our dbb
for (ULONG offset = alignOffset(sizeof(Header)); offset < sh_mem_header->used;)
{
UCHAR* const ptr = (UCHAR*) sh_mem_header + offset;
const Element* const element = (Element*) ptr;
const ULONG length = alignOffset(sizeof(Element) + element->length);
if (element->processId == process_id && element->localId == local_id)
{
fb_assert(sh_mem_header->used >= offset + length);
memmove(ptr, ptr + length, sh_mem_header->used - offset - length);
sh_mem_header->used -= length;
}
else
{
offset += length;
}
}
}
void DatabaseSnapshot::SharedData::ensureSpace(ULONG length)
{
ULONG newSize = sh_mem_header->used + length;
if (newSize > sh_mem_header->allocated)
{
newSize = FB_ALIGN(newSize, DEFAULT_SIZE);
#ifdef HAVE_OBJECT_MAP
Arg::StatusVector statusVector;
if (!remapFile(statusVector, newSize, true))
{
status_exception::raise(statusVector);
}
sh_mem_header->allocated = sh_mem_length_mapped;
#else
status_exception::raise(Arg::Gds(isc_montabexh));
#endif
}
}
void DatabaseSnapshot::SharedData::mutexBug(int osErrorCode, const char* string)
{
gds__log("MONITOR: mutex %s error, status = %d", string, osErrorCode);
//fprintf(stderr, "%s\n", msg);
exit(FINI_ERROR);
}
bool DatabaseSnapshot::SharedData::initialize(bool initialize)
{
if (initialize)
{
// Initialize the shared data header
sh_mem_header->mhb_type = SRAM_DATABASE_SNAPSHOT;
sh_mem_header->mhb_version = MONITOR_VERSION;
sh_mem_header->used = alignOffset(sizeof(Header));
sh_mem_header->allocated = sh_mem_length_mapped;
}
return true;
}
ULONG DatabaseSnapshot::SharedData::alignOffset(ULONG unaligned)
{
return Firebird::MEM_ALIGN(unaligned);
}
// DatabaseSnapshot class
DatabaseSnapshot* DatabaseSnapshot::create(thread_db* tdbb)
{
SET_TDBB(tdbb);
jrd_tra* transaction = tdbb->getTransaction();
fb_assert(transaction);
if (!transaction->tra_db_snapshot)
{
// Create a database snapshot and store it
// in the transaction block
MemoryPool& pool = *transaction->tra_pool;
transaction->tra_db_snapshot = FB_NEW(pool) DatabaseSnapshot(tdbb, pool);
}
return transaction->tra_db_snapshot;
}
int DatabaseSnapshot::blockingAst(void* ast_object)
{
Database* dbb = static_cast<Database*>(ast_object);
try
{
Lock* const lock = dbb->dbb_monitor_lock;
Database::SyncGuard dsGuard(dbb, true);
ThreadContextHolder tdbb;
tdbb->setDatabase(lock->lck_dbb);
tdbb->setAttachment(lock->lck_attachment);
ContextPoolHolder context(tdbb, dbb->dbb_permanent);
if (!(dbb->dbb_ast_flags & DBB_monitor_off))
{
// Write the data to the shared memory
if (!(dbb->dbb_flags & DBB_not_in_use))
{
try
{
dumpData(tdbb);
}
catch (const Exception& ex)
{
iscLogException("Cannot dump the monitoring data", ex);
}
}
// Release the lock and mark dbb as requesting a new one
LCK_release(tdbb, lock);
dbb->dbb_ast_flags |= DBB_monitor_off;
}
}
catch (const Exception&)
{} // no-op
return 0;
}
DatabaseSnapshot::DatabaseSnapshot(thread_db* tdbb, MemoryPool& pool)
: snapshot(pool), idMap(pool), idCounter(0)
{
SET_TDBB(tdbb);
PAG_header(tdbb, true);
Database* const dbb = tdbb->getDatabase();
fb_assert(dbb);
// Initialize record buffers
RecordBuffer* const dbb_buffer = allocBuffer(tdbb, pool, rel_mon_database);
RecordBuffer* const att_buffer = allocBuffer(tdbb, pool, rel_mon_attachments);
RecordBuffer* const tra_buffer = allocBuffer(tdbb, pool, rel_mon_transactions);
RecordBuffer* const stmt_buffer = allocBuffer(tdbb, pool, rel_mon_statements);
RecordBuffer* const call_buffer = allocBuffer(tdbb, pool, rel_mon_calls);
RecordBuffer* const io_stat_buffer = allocBuffer(tdbb, pool, rel_mon_io_stats);
RecordBuffer* const rec_stat_buffer = allocBuffer(tdbb, pool, rel_mon_rec_stats);
RecordBuffer* const ctx_var_buffer = allocBuffer(tdbb, pool, rel_mon_ctx_vars);
RecordBuffer* const mem_usage_buffer = allocBuffer(tdbb, pool, rel_mon_mem_usage);
// Release our own lock
LCK_release(tdbb, dbb->dbb_monitor_lock);
dbb->dbb_ast_flags &= ~DBB_monitor_off;
{ // scope for the RAII object
// Ensure we'll be dealing with a valid backup state inside the call below
BackupManager::StateReadGuard holder(tdbb);
// Dump our own data
dumpData(tdbb);
}
// Signal other processes to dump their data
Lock temp_lock, *lock = &temp_lock;
lock->lck_dbb = dbb;
lock->lck_length = sizeof(SLONG);
lock->lck_key.lck_long = 0;
lock->lck_type = LCK_monitor;
lock->lck_owner_handle = LCK_get_owner_handle(tdbb, lock->lck_type);
lock->lck_parent = dbb->dbb_lock;
if (LCK_lock(tdbb, lock, LCK_EX, LCK_WAIT))
LCK_release(tdbb, lock);
// Mark dbb as requesting a new lock
dbb->dbb_ast_flags |= DBB_monitor_off;
// Read the shared memory
ULONG dataSize = 0;
UCHAR* dataPtr = NULL;
{ // scope
fb_assert(dbb->dbb_monitoring_data);
DumpGuard guard(dbb->dbb_monitoring_data);
dataPtr = dbb->dbb_monitoring_data->read(pool, dataSize);
}
fb_assert(dataSize && dataPtr);
AutoPtr<UCHAR, ArrayDelete<UCHAR> > data(dataPtr);
Reader reader(dataSize, data);
const Attachment* const attachment = tdbb->getAttachment();
fb_assert(attachment);
string databaseName(dbb->dbb_database_name.c_str());
ISC_systemToUtf8(databaseName);
const string& userName = attachment->att_user->usr_user_name;
const bool locksmith = attachment->locksmith();
// Parse the dump
RecordBuffer* buffer = NULL;
Record* record = NULL;
bool dbb_processed = false, fields_processed = false;
bool dbb_allowed = false, att_allowed = false;
int attachment_charset = ttype_none;
DumpRecord dumpRecord;
while (reader.getRecord(dumpRecord))
{
const int rid = dumpRecord.getRelationId();
switch (rid)
{
case rel_mon_database:
buffer = dbb_buffer;
break;
case rel_mon_attachments:
buffer = att_buffer;
break;
case rel_mon_transactions:
buffer = tra_buffer;
break;
case rel_mon_statements:
buffer = stmt_buffer;
break;
case rel_mon_calls:
buffer = call_buffer;
break;
case rel_mon_io_stats:
buffer = io_stat_buffer;
break;
case rel_mon_rec_stats:
buffer = rec_stat_buffer;
break;
case rel_mon_ctx_vars:
buffer = ctx_var_buffer;
break;
case rel_mon_mem_usage:
buffer = mem_usage_buffer;
break;
default:
fb_assert(false);
}
if (buffer)
{
record = buffer->getTempRecord();
clearRecord(record);
}
else
{
record = NULL;
}
DumpField dumpField;
while (dumpRecord.getField(dumpField))
{
const USHORT fid = dumpField.id;
const size_t length = dumpField.length;
const char* source = (const char*) dumpField.data;
bool set_charset = false;
// special case for MON$DATABASE
if (rid == rel_mon_database)
{
if (fid == f_mon_db_name)
{
dbb_allowed = !databaseName.compare(source, length);
}
if (record && dbb_allowed && !dbb_processed)
{
putField(tdbb, record, dumpField, attachment_charset);
fields_processed = true;
}
att_allowed = (dbb_allowed && !dbb_processed);
}
// special case for MON$ATTACHMENTS
else if (rid == rel_mon_attachments)
{
if (fid == f_mon_att_user)
{
attachment_charset = ttype_none;
att_allowed = locksmith || !userName.compare(source, length);
}
else if (fid == f_mon_att_charset_id)
{
set_charset = true;
}
if (record && dbb_allowed && att_allowed)
{
putField(tdbb, record, dumpField, attachment_charset, set_charset);
fields_processed = true;
dbb_processed = true;
}
}
// generic logic that covers all other relations
else if (record && dbb_allowed && att_allowed)
{
putField(tdbb, record, dumpField, attachment_charset);
fields_processed = true;
dbb_processed = true;
}
}
if (fields_processed)
{
buffer->store(record);
fields_processed = false;
}
}
}
DatabaseSnapshot::~DatabaseSnapshot()
{
for (size_t i = 0; i < snapshot.getCount(); i++)
{
delete snapshot[i].data;
}
}
RecordBuffer* DatabaseSnapshot::getData(const jrd_rel* relation) const
{
fb_assert(relation);
for (size_t i = 0; i < snapshot.getCount(); i++)
{
if (snapshot[i].rel_id == relation->rel_id)
return snapshot[i].data;
}
return NULL;
}
RecordBuffer* DatabaseSnapshot::allocBuffer(thread_db* tdbb, MemoryPool& pool, int rel_id)
{
jrd_rel* relation = MET_lookup_relation_id(tdbb, rel_id, false);
fb_assert(relation);
MET_scan_relation(tdbb, relation);
fb_assert(relation->isVirtual());
Format* format = MET_current(tdbb, relation);
fb_assert(format);
RecordBuffer* buffer = FB_NEW(pool) RecordBuffer(pool, format);
RelationData data = {relation->rel_id, buffer};
snapshot.add(data);
return buffer;
}
void DatabaseSnapshot::clearRecord(Record* record)
{
fb_assert(record);
// Initialize all fields to NULLs
memset(record->rec_data, 0, record->rec_length);
const size_t null_bytes = (record->rec_format->fmt_count + 7u) >> 3;
memset(record->rec_data, 0xFF, null_bytes);
}
void DatabaseSnapshot::putField(thread_db* tdbb, Record* record, const DumpField& field,
int& charset, bool set_charset)
{
fb_assert(record);
const Format* const format = record->rec_format;
fb_assert(format && field.id < format->fmt_count);
dsc to_desc = format->fmt_desc[field.id];
to_desc.dsc_address += (IPTR) record->rec_data;
if (field.type == VALUE_GLOBAL_ID)
{
// special case: translate 64-bit global ID into 32-bit local ID
fb_assert(field.length == sizeof(SINT64));
SINT64 global_id;
memcpy(&global_id, field.data, field.length);
SLONG local_id;
if (!idMap.get(global_id, local_id))
{
local_id = ++idCounter;
idMap.put(global_id, local_id);
}
dsc from_desc;
from_desc.makeLong(0, &local_id);
MOV_move(tdbb, &from_desc, &to_desc);
}
else if (field.type == VALUE_INTEGER)
{
fb_assert(field.length == sizeof(SINT64));
SINT64 value;
memcpy(&value, field.data, field.length);
dsc from_desc;
from_desc.makeInt64(0, &value);
MOV_move(tdbb, &from_desc, &to_desc);
if (set_charset)
charset = (int) value;
}
else if (field.type == VALUE_TIMESTAMP)
{
fb_assert(field.length == sizeof(ISC_TIMESTAMP));
ISC_TIMESTAMP value;
memcpy(&value, field.data, field.length);
dsc from_desc;
from_desc.makeTimestamp(&value);
MOV_move(tdbb, &from_desc, &to_desc);
}
else if (field.type == VALUE_STRING)
{
dsc from_desc;
MoveBuffer buffer;
if (charset == CS_NONE && to_desc.getCharSet() == CS_METADATA)
{
// ASF: If an attachment using NONE charset has a string using non-ASCII characters,
// nobody will be able to select them in a system field. So we change these characters to
// question marks here - CORE-2602.
UCHAR* p = buffer.getBuffer(field.length);
const UCHAR* s = (const UCHAR*) field.data;
for (const UCHAR* end = buffer.end(); p < end; ++p, ++s)
*p = (*s > 0x7F ? '?' : *s);
from_desc.makeText(field.length, CS_ASCII, buffer.begin());
}
else
from_desc.makeText(field.length, charset, (UCHAR*) field.data);
MOV_move(tdbb, &from_desc, &to_desc);
}
else
{
fb_assert(false);
}
// hvlad: detach just created temporary blob from request to bound its
// lifetime to transaction. This is necessary as this blob belongs to
// the MON$ table and must be accessible until transaction ends.
if (to_desc.isBlob())
{
bid* blob_id = reinterpret_cast<bid*>(to_desc.dsc_address);
jrd_tra* tran = tdbb->getTransaction();
const bool traFound = tran->tra_blobs->locate(blob_id->bid_temp_id());
fb_assert(traFound);
BlobIndex& blobIdx = tran->tra_blobs->current();
fb_assert(!blobIdx.bli_materialized);
if (blobIdx.bli_request)
{
const bool reqFound = blobIdx.bli_request->req_blobs.locate(blobIdx.bli_temp_id);
fb_assert(reqFound);
blobIdx.bli_request->req_blobs.fastRemove();
blobIdx.bli_request = NULL;
}
}
CLEAR_NULL(record, field.id);
}
void DatabaseSnapshot::dumpData(thread_db* tdbb)
{
fb_assert(tdbb);
Database* const dbb = tdbb->getDatabase();
fb_assert(dbb);
if (!dbb->dbb_monitoring_data)
{
dbb->dbb_monitoring_data = FB_NEW(*dbb->dbb_permanent) SharedData(dbb);
}
DumpGuard guard(dbb->dbb_monitoring_data);
dbb->dbb_monitoring_data->cleanup();
Writer writer(dbb->dbb_monitoring_data);
// Database information
putDatabase(dbb, writer, fb_utils::genUniqueId());
// Attachment information
for (Attachment* attachment = dbb->dbb_attachments; attachment; attachment = attachment->att_next)
{
if (!putAttachment(tdbb, attachment, writer, fb_utils::genUniqueId()))
continue;
putContextVars(attachment->att_context_vars, writer, attachment->att_attachment_id, true);
jrd_tra* transaction = NULL;
jrd_req* request = NULL;
// Transaction information
for (transaction = attachment->att_transactions;
transaction; transaction = transaction->tra_next)
{
putTransaction(transaction, writer, fb_utils::genUniqueId());
putContextVars(transaction->tra_context_vars, writer, transaction->tra_number, false);
}
// Call stack information
for (transaction = attachment->att_transactions;
transaction; transaction = transaction->tra_next)
{
for (request = transaction->tra_requests; request; request = request->req_caller)
{
request->adjustCallerStats();
if (!(request->getStatement()->flags &
(JrdStatement::FLAG_INTERNAL | JrdStatement::FLAG_SYS_TRIGGER)) &&
request->req_caller)
{
putCall(request, writer, fb_utils::genUniqueId());
}
}
}
// Request information
for (const jrd_req* const* i = attachment->att_requests.begin();
i != attachment->att_requests.end();
++i)
{
const jrd_req* request = *i;
if (!(request->getStatement()->flags &
(JrdStatement::FLAG_INTERNAL | JrdStatement::FLAG_SYS_TRIGGER)))
{
putRequest(request, writer, fb_utils::genUniqueId());
}
}
}
}
SINT64 DatabaseSnapshot::getGlobalId(int value)
{
return ((SINT64) getpid() << BITS_PER_LONG) + value;
}
void DatabaseSnapshot::putDatabase(const Database* database, Writer& writer, int stat_id)
{
fb_assert(database);
DumpRecord record(rel_mon_database);
PathName databaseName(*getDefaultMemoryPool(), database->dbb_database_name);
ISC_systemToUtf8(databaseName);
// database name or alias (MUST BE ALWAYS THE FIRST ITEM PASSED!)
record.storeString(f_mon_db_name, databaseName);
// page size
record.storeInteger(f_mon_db_page_size, database->dbb_page_size);
// major ODS version
record.storeInteger(f_mon_db_ods_major, database->dbb_ods_version);
// minor ODS version
record.storeInteger(f_mon_db_ods_minor, database->dbb_minor_version);
// oldest interesting transaction
record.storeInteger(f_mon_db_oit, database->dbb_oldest_transaction);
// oldest active transaction
record.storeInteger(f_mon_db_oat, database->dbb_oldest_active);
// oldest snapshot transaction
record.storeInteger(f_mon_db_ost, database->dbb_oldest_snapshot);
// next transaction
record.storeInteger(f_mon_db_nt, database->dbb_next_transaction);
// number of page buffers
record.storeInteger(f_mon_db_page_bufs, database->dbb_bcb->bcb_count);
int temp;
// SQL dialect
temp = (database->dbb_flags & DBB_DB_SQL_dialect_3) ? 3 : 1;
record.storeInteger(f_mon_db_dialect, temp);
// shutdown mode
if (database->dbb_ast_flags & DBB_shutdown_full)
{
temp = shut_mode_full;
}
else if (database->dbb_ast_flags & DBB_shutdown_single)
{
temp = shut_mode_single;
}
else if (database->dbb_ast_flags & DBB_shutdown)
{
temp = shut_mode_multi;
}
else
{
temp = shut_mode_online;
}
record.storeInteger(f_mon_db_shut_mode, temp);
// sweep interval
record.storeInteger(f_mon_db_sweep_int, database->dbb_sweep_interval);
// read only flag
temp = (database->dbb_flags & DBB_read_only) ? 1 : 0;
record.storeInteger(f_mon_db_read_only, temp);
// forced writes flag
temp = (database->dbb_flags & DBB_force_write) ? 1 : 0;
record.storeInteger(f_mon_db_forced_writes, temp);
// reserve space flag
temp = (database->dbb_flags & DBB_no_reserve) ? 0 : 1;
record.storeInteger(f_mon_db_res_space, temp);
// creation date
record.storeTimestamp(f_mon_db_created, database->dbb_creation_date);
// database size
record.storeInteger(f_mon_db_pages, PageSpace::actAlloc(database));
// database state
switch (database->dbb_backup_manager->getState())
{
case nbak_state_normal:
temp = backup_state_normal;
break;
case nbak_state_stalled:
temp = backup_state_stalled;
break;
case nbak_state_merge:
temp = backup_state_merge;
break;
default:
temp = backup_state_unknown;
}
record.storeInteger(f_mon_db_backup_state, temp);
// statistics
record.storeGlobalId(f_mon_db_stat_id, getGlobalId(stat_id));
writer.putRecord(record);
putStatistics(database->dbb_stats, writer, stat_id, stat_database);
if (Config::getSharedCache())
{
putMemoryUsage(database->dbb_memory_stats, writer, stat_id, stat_database);
}
else
{
MemoryStats zero_stats;
putMemoryUsage(zero_stats, writer, stat_id, stat_database);
}
}
bool DatabaseSnapshot::putAttachment(thread_db* tdbb, const Jrd::Attachment* attachment,
Writer& writer, int stat_id)
{
fb_assert(attachment);
if (!attachment->att_user)
{
return false;
}
DumpRecord record(rel_mon_attachments);
int temp = mon_state_idle;
for (const jrd_tra* transaction_itr = attachment->att_transactions;
transaction_itr; transaction_itr = transaction_itr->tra_next)
{
if (transaction_itr->tra_requests)
{
temp = mon_state_active;
break;
}
}
PathName attName(*getDefaultMemoryPool(), attachment->att_filename);
ISC_systemToUtf8(attName);
// user (MUST BE ALWAYS THE FIRST ITEM PASSED!)
record.storeString(f_mon_att_user, attachment->att_user->usr_user_name);
// attachment id
record.storeInteger(f_mon_att_id, attachment->att_attachment_id);
// process id
record.storeInteger(f_mon_att_server_pid, getpid());
// state
record.storeInteger(f_mon_att_state, temp);
// attachment name
record.storeString(f_mon_att_name, attName);
// role
record.storeString(f_mon_att_role, attachment->att_user->usr_sql_role_name);
// remote protocol
record.storeString(f_mon_att_remote_proto, attachment->att_network_protocol);
// remote address
record.storeString(f_mon_att_remote_addr, attachment->att_remote_address);
// remote process id
if (attachment->att_remote_pid)
{
record.storeInteger(f_mon_att_remote_pid, attachment->att_remote_pid);
}
// remote process name
record.storeString(f_mon_att_remote_process, attachment->att_remote_process);
// charset
record.storeInteger(f_mon_att_charset_id, attachment->att_charset);
// timestamp
record.storeTimestamp(f_mon_att_timestamp, attachment->att_timestamp);
// garbage collection flag
temp = (attachment->att_flags & ATT_no_cleanup) ? 0 : 1;
record.storeInteger(f_mon_att_gc, temp);
// statistics
record.storeGlobalId(f_mon_att_stat_id, getGlobalId(stat_id));
writer.putRecord(record);
putStatistics(attachment->att_stats, writer, stat_id, stat_attachment);
if (Config::getSharedCache())
{
putMemoryUsage(attachment->att_memory_stats, writer, stat_id, stat_attachment);
}
else
{
putMemoryUsage(attachment->att_database->dbb_memory_stats, writer, stat_id, stat_attachment);
}
return true;
}
void DatabaseSnapshot::putTransaction(const jrd_tra* transaction, Writer& writer, int stat_id)
{
fb_assert(transaction);
DumpRecord record(rel_mon_transactions);
int temp;
// transaction id
record.storeInteger(f_mon_tra_id, transaction->tra_number);
// attachment id
record.storeInteger(f_mon_tra_att_id, transaction->tra_attachment->att_attachment_id);
// state
temp = transaction->tra_requests ? mon_state_active : mon_state_idle;
record.storeInteger(f_mon_tra_state, temp);
// timestamp
record.storeTimestamp(f_mon_tra_timestamp, transaction->tra_timestamp);
// top transaction