Skip to content

Commit c25b7ee

Browse files
authored
fix memory leaks (#14712)
1 parent f2c5cc1 commit c25b7ee

File tree

7 files changed

+103
-86
lines changed

7 files changed

+103
-86
lines changed

ydb/apps/ydb/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Fixed memory leak in tpcds generator.
12
* Include external data sources and external tables in local backups (`ydb tools dump` and `ydb tools restore`). Both scheme objects are backed up as YQL creation queries saved in the `create_external_data_source.sql` and `create_external_table.sql` files respectively, which can be executed to recreate the original scheme objects.
23
* Fixed a bug where `ydb auth get-token` command tried to authenticate twice: while listing andpoints and while executing actual token request.
34
* Fixed a bug where `ydb import file csv` command was saving progress even if a batch upload had been failed.

ydb/library/benchmarks/gen/tpcds-dbgen/w_customer.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ mk_w_customer (void * row, ds_key_t index)
9090

9191
if (!bInit)
9292
{
93-
nBaseDate = dttoj (strtodate (DATE_MINIMUM));
93+
date_t* minDate = strtodate (DATE_MINIMUM);
94+
nBaseDate = dttoj (minDate);
95+
free(minDate);
9496
strtodt(&dtBirthMax, "1992-12-31");
9597
strtodt(&dtBirthMin, "1924-01-01");
9698
strtodt(&dtToday, TODAYS_DATE);

ydb/library/benchmarks/gen/tpcds-dbgen/w_inventory.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ mk_w_inventory(void *pDest, ds_key_t index)
9090
base_date = strtodate (DATE_MINIMUM);
9191
jDate = base_date->julian;
9292
set_dow(base_date);
93+
free(base_date);
9394
/* Make exceptions to the 1-rng-call-per-row rule */
9495
bInit = 1;
9596
}

ydb/library/benchmarks/gen/tpcds-dbgen/w_store.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ mk_w_store (void* row, ds_key_t index)
8181
nDaysOpen,
8282
nMin,
8383
nMax;
84-
static date_t *tDate;
84+
static date_t tDate;
8585
static decimal_t min_rev_growth,
8686
max_rev_growth,
8787
dMinTaxPercentage,
@@ -101,7 +101,9 @@ if (!bInit)
101101
{
102102
nHierarchyTotal = (int) get_rowcount (DIVISIONS);
103103
nHierarchyTotal *= (int) get_rowcount (COMPANY);
104-
tDate = strtodate (DATE_MINIMUM);
104+
date_t* minDate = strtodate (DATE_MINIMUM);
105+
tDate = *minDate;
106+
free (minDate);
105107
strtodec (&min_rev_growth, STORE_MIN_REV_GROWTH);
106108
strtodec (&max_rev_growth, STORE_MAX_REV_GROWTH);
107109
strtodec (&dRevMin, "1.00");
@@ -111,6 +113,7 @@ if (!bInit)
111113

112114
/* columns that should be dynamic */
113115
r->rec_end_date_id = -1;
116+
bInit = 1;
114117
}
115118

116119
nullSet(&pT->kNullBitMap, W_STORE_NULLS);
@@ -139,7 +142,7 @@ if (!bInit)
139142
genrand_integer (NULL, DIST_UNIFORM, STORE_MIN_DAYS_OPEN, STORE_MAX_DAYS_OPEN, 0,
140143
W_STORE_CLOSED_DATE_ID);
141144
if (nPercentage < STORE_CLOSED_PCT)
142-
r->closed_date_id = tDate->julian + nDaysOpen;
145+
r->closed_date_id = tDate.julian + nDaysOpen;
143146
else
144147
r->closed_date_id = -1;
145148
changeSCD(SCD_KEY, &r->closed_date_id, &rOldValues->closed_date_id, &nFieldChangeFlags, bFirstRecord);

ydb/library/workload/tpcds/dg_catalog_sales.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ class TCatalogSalesGenerator {
2121
public:
2222
void MakeMaster(ds_key_t index) {
2323
int giftPct;
24-
static bool init = false;
25-
if (!init) {
24+
if (!ItemPermutation) {
2625
Date = skipDays(CATALOG_SALES, &NewDateIndex);
2726
ItemPermutation = makePermutation(NULL, (ItemCount = (int)getIDCount(ITEM)), CS_PERMUTE);
28-
init = true;
2927
}
3028

3129
while (index > NewDateIndex) {
@@ -91,10 +89,16 @@ class TCatalogSalesGenerator {
9189
writerSales.RegisterRow();
9290
}
9391

92+
~TCatalogSalesGenerator() {
93+
if (ItemPermutation) {
94+
free(ItemPermutation);
95+
}
96+
}
97+
9498
private:
9599
int TicketItemBase = 1;
96100
int ItemCount;
97-
int* ItemPermutation;
101+
int* ItemPermutation = nullptr;
98102
ds_key_t NewDateIndex = 0;
99103
ds_key_t Date;
100104
};

ydb/library/workload/tpcds/dg_store_sales.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ namespace NYdbWorkload {
2020
class TStoreSalesGenerator {
2121
public:
2222
void MakeMaster(ds_key_t index) {
23-
static bool init = false;
24-
if (!init) {
23+
if (!ItemPermutation) {
2524
Date = skipDays(STORE_SALES, &NewDateIndex);
2625
ItemPermutation = makePermutation(NULL, ItemCount = (int)getIDCount(ITEM), SS_PERMUTATION);
27-
init = true;
2826
}
2927

3028
while (index > NewDateIndex) {
@@ -65,10 +63,15 @@ class TStoreSalesGenerator {
6563
writerSales.RegisterRow();
6664
}
6765

66+
~TStoreSalesGenerator() {
67+
if (ItemPermutation) {
68+
free(ItemPermutation);
69+
}
70+
}
6871
private:
6972
int ItemCount;
7073
int ItemIndex;
71-
int* ItemPermutation;
74+
int* ItemPermutation = nullptr;
7275
ds_key_t NewDateIndex = 0;
7376
ds_key_t Date;
7477
};

ydb/library/workload/tpcds/driver.cpp

Lines changed: 77 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,63 +18,85 @@ extern "C" {
1818

1919
#define FL_LOADED 0x01
2020

21-
extern "C" int di_compare(const void *op1, const void *op2) {
22-
d_idx_t *ie1 = (d_idx_t *)op1,
23-
*ie2 = (d_idx_t *)op2;
24-
25-
return strcasecmp(ie1->name, ie2->name);
26-
}
21+
struct TDist: public dist_t {
22+
TVector<int> TypeVector;
23+
TVector<int> WeightSets;
24+
TVector<int*> WeightSetIndex;
25+
TVector<int> Maximums;
26+
TVector<int> ValueSets;
27+
TVector<int*> ValueSetIndex;
28+
TString Strings;
29+
TString Names;
30+
};
2731

28-
int load_dist(d_idx_t *di) {
29-
int res = 0;
30-
int32_t temp;
32+
struct TDIdx: public d_idx_t {
33+
explicit TDIdx(IInputStream& mi) {
34+
memset(this, 0, sizeof(d_idx_t));
35+
mi.Read(name, D_NAME_LEN);
36+
name[D_NAME_LEN] = '\0';
37+
index = ReadInt(mi);
38+
offset = ReadInt(mi);
39+
str_space = ReadInt(mi);
40+
length = ReadInt(mi);
41+
w_width = ReadInt(mi);
42+
v_width = ReadInt(mi);
43+
name_space = ReadInt(mi);
44+
dist = nullptr;
45+
}
3146

32-
if (di->flags != FL_LOADED) {
47+
void Load() {
48+
dist = &Dist;
3349
auto resource = NResource::Find("tpcds.idx");
3450
TStringInput mi(resource);
35-
mi.Skip(di->offset);
36-
di->dist = (dist_t *)malloc(sizeof(struct DIST_T));
37-
auto d = di->dist;
38-
d->type_vector = (int *)malloc(sizeof(int32_t) * di->v_width);
39-
for (int i = 0; i < di->v_width; i++) {
40-
mi.Read(&temp, sizeof(int32_t));
41-
d->type_vector[i] = ntohl(temp);
51+
mi.Skip(offset);
52+
Dist.TypeVector.resize(v_width);
53+
Dist.type_vector = Dist.TypeVector.data();
54+
for (int i = 0; i < v_width; i++) {
55+
Dist.TypeVector[i] = ReadInt(mi);
4256
}
43-
44-
d->weight_sets = (int **)malloc(sizeof(int *) * di->w_width);
45-
d->maximums = (int *)malloc(sizeof(int32_t) * di->w_width);
46-
for (int i = 0; i < di->w_width; i++) {
47-
*(d->weight_sets + i) = (int *)malloc(di->length * sizeof(int32_t));
48-
d->maximums[i] = 0;
49-
for (int j = 0; j < di->length; j++) {
50-
mi.Read(&temp, sizeof(int32_t));
51-
*(*(d->weight_sets + i) + j) = ntohl(temp);
52-
d->maximums[i] += d->weight_sets[i][j];
53-
d->weight_sets[i][j] = d->maximums[i];
57+
Dist.WeightSetIndex.resize(w_width);
58+
Dist.WeightSets.resize(w_width * length);
59+
Dist.weight_sets = Dist.WeightSetIndex.data();
60+
Dist.Maximums.resize(w_width);
61+
Dist.maximums = Dist.Maximums.data();
62+
for (int i = 0; i < w_width; i++) {
63+
Dist.Maximums[i] = 0;
64+
Dist.WeightSetIndex[i] = Dist.WeightSets.data() + i * length;
65+
for (int j = 0; j < length; j++) {
66+
Dist.maximums[i] += ReadInt(mi);
67+
Dist.weight_sets[i][j] = Dist.maximums[i];
5468
}
5569
}
56-
57-
d->value_sets = (int **)malloc(sizeof(int *) * di->v_width);
58-
MALLOC_CHECK(d->value_sets);
59-
for (int i = 0; i < di->v_width; i++) {
60-
*(d->value_sets + i) = (int *)malloc(di->length * sizeof(int32_t));
61-
for (int j = 0; j < di->length; j++) {
62-
mi.Read(&temp, sizeof(int32_t));
63-
*(*(d->value_sets + i) + j) = ntohl(temp);
70+
71+
Dist.ValueSetIndex.resize(v_width);
72+
Dist.ValueSets.resize(v_width * length);
73+
Dist.value_sets = Dist.ValueSetIndex.data();
74+
for (int i = 0; i < v_width; i++) {
75+
Dist.ValueSetIndex[i] = Dist.ValueSets.data() + i * length;
76+
for (int j = 0; j < length; j++) {
77+
Dist.value_sets[i][j] = ReadInt(mi);
6478
}
6579
}
6680

67-
if (di->name_space) {
68-
d->names = (char *)malloc(di->name_space);
69-
mi.Read(d->names, di->name_space * sizeof(char));
81+
if (name_space) {
82+
Dist.Names.resize(name_space);
83+
Dist.names = Dist.Names.begin();
84+
mi.Read(Dist.names, name_space * sizeof(char));
7085
}
7186

72-
d->strings = (char *)malloc(sizeof(char) * di->str_space);
73-
mi.Read(d->strings, di->str_space * sizeof(char));
74-
di->flags = FL_LOADED;
87+
Dist.Strings.resize(str_space);
88+
Dist.strings = Dist.Strings.begin();
89+
mi.Read(Dist.strings, str_space * sizeof(char));
90+
flags = FL_LOADED;
7591
}
76-
return(res);
77-
}
92+
93+
static inline int ReadInt(IInputStream& mi) {
94+
int temp;
95+
mi.Read(&temp, sizeof(int32_t));
96+
return ntohl(temp);
97+
};
98+
TDist Dist;
99+
};
78100

79101

80102
class TDists {
@@ -85,44 +107,25 @@ class TDists {
85107
memcpy(&temp, resource.data(), sizeof(int32_t));
86108
int entry_count = ntohl(temp);
87109
TMemoryInput mi(resource.end() - entry_count * IDX_SIZE, entry_count * IDX_SIZE);
88-
Idxs.resize(entry_count);
89-
for (auto& current_idx: Idxs) {
90-
memset(&current_idx, 0, sizeof(d_idx_t));
91-
mi.Read(current_idx.name, D_NAME_LEN);
92-
current_idx.name[D_NAME_LEN] = '\0';
93-
mi.Read(&temp, sizeof(int32_t));
94-
current_idx.index = ntohl(temp);
95-
mi.Read(&temp, sizeof(int32_t));
96-
current_idx.offset = ntohl(temp);
97-
mi.Read(&temp, sizeof(int32_t));
98-
current_idx.str_space = ntohl(temp);
99-
mi.Read(&temp, sizeof(int32_t));
100-
current_idx.length = ntohl(temp);
101-
mi.Read(&temp, sizeof(int32_t));
102-
current_idx.w_width = ntohl(temp);
103-
mi.Read(&temp, sizeof(int32_t));
104-
current_idx.v_width = ntohl(temp);
105-
mi.Read(&temp, sizeof(int32_t));
106-
current_idx.name_space = ntohl(temp);
107-
current_idx.dist = NULL;
110+
for (auto i = 0; i < entry_count; ++i) {
111+
TDIdx current_idx(mi);
112+
TString name(current_idx.name);
113+
name.to_lower();
114+
Idxs.emplace(name, std::move(current_idx));
108115
}
109-
qsort(Idxs.begin(), entry_count, sizeof(d_idx_t), di_compare);
110116
}
111117

112-
d_idx_t* operator[](char* name) {
113-
d_idx_t key;
114-
strcpy(key.name, name);
115-
auto id = (d_idx_t *)bsearch(&key, Idxs.begin(), Idxs.size(), sizeof(d_idx_t), di_compare);
116-
if (id != NULL) {
117-
if (id->flags != FL_LOADED) {
118-
load_dist(id);
119-
}
118+
d_idx_t* operator[](TString name) {
119+
name.to_lower();
120+
auto id = MapFindPtr(Idxs, name);
121+
if (id != NULL && id->flags != FL_LOADED) {
122+
id->Load();
120123
}
121124
return id;
122125
}
123126

124127
private:
125-
TVector<d_idx_t> Idxs;
128+
TMap<TString, TDIdx> Idxs;
126129
};
127130

128131
extern "C" d_idx_t* find_dist(char *name) {

0 commit comments

Comments
 (0)