cachelab started

This commit is contained in:
2025-04-21 23:52:27 +08:00
parent cc99d9b5d9
commit ace7a46fb9
58 changed files with 10071 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
#
# Copyright 2019 Yuta Hirokawa (University of Tsukuba, Japan)
#
# Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
add_executable(simple_usage simple_usage.c)
add_dependencies(simple_usage ${CBSL_LIB})
target_link_libraries(simple_usage ${CBSL_LIB} ${ZSTD_LIB})
add_executable(recommend_usage recommend_usage.c)
add_dependencies(recommend_usage ${CBSL_LIB})
target_link_libraries(recommend_usage ${CBSL_LIB} ${ZSTD_LIB})

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2019 Yuta Hirokawa (University of Tsukuba, Japan)
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cbsl.h>
#define CBSL_ERROR_CHECK(X) {if ((X) == cbsl_error) { fprintf(stderr, "error: %s\n", (#X)); exit(1); }}
#define CHECK_BINARY(X,Y) {if (memcmp(&(X),&(Y),sizeof((X))) != 0) { fprintf(stderr, "error: binary check %s == %s\n", (#X), (#Y)); exit(1); }}
#define CHECK(X) {if (!(X)) { fprintf(stderr, "error: %s\n", (#X)); }}
#define SAFE_FREE(X) {if ((X) != NULL) { free((X)); }}
static const int data0 = 43;
static const double data1 = 3.14159265;
static const int data2 = 14142;
extern void checkpoint_restart(cbsl_mode, int*, double*, int*, int**);
char cname[128];
int main(int argc, char** argv)
{
int c0;
double c1;
int c2;
int* a0;
if (argc < 2)
return 1;
sprintf(cname, "checkpoint.zst");
cbsl_mode mode = cbsl_unknown_mode;
if (strcmp(argv[1],"-c") == 0)
{
mode = cbsl_store_mode;
c0 = data0;
c1 = data1;
c2 = data2;
a0 = (int*)(malloc(sizeof(int) * 42));
for (int i = 0; i < 42; ++i)
a0[i] = 42;
}
else if (strcmp(argv[1],"-r") == 0)
{
mode = cbsl_load_mode;
c0 = c1 = c2 = 0;
a0 = NULL;
}
else
{
return 1;
}
checkpoint_restart(mode, &c0, &c1, &c2, &a0);
CHECK_BINARY(c0, data0);
CHECK_BINARY(c1, data1);
CHECK_BINARY(c2, data2);
for (int i = 0; i < 42; ++i)
{
CHECK(a0[i] == 42);
}
SAFE_FREE(a0);
return 0;
}
void checkpoint_restart(cbsl_mode mode, int* c0, double* c1, int* c2, int** a0)
{
int a0_size;
cbsl_ctx* ctx = cbsl_open(mode, cname);
if (ctx == NULL)
{
fprintf(stderr, "error: cbsl_open\n");
exit(1);
}
CBSL_ERROR_CHECK(cbsl_record(ctx, c0, sizeof(int)));
CBSL_ERROR_CHECK(cbsl_record(ctx, c1, sizeof(double)));
CBSL_ERROR_CHECK(cbsl_record(ctx, c2, sizeof(int)));
CBSL_ERROR_CHECK(cbsl_record(ctx, &a0_size, sizeof(int)));
if (mode == cbsl_load_mode)
(*a0) = (int*)(malloc(sizeof(int) * a0_size));
CBSL_ERROR_CHECK(cbsl_record(ctx, (*a0), sizeof(int) * a0_size));
CBSL_ERROR_CHECK(cbsl_close(ctx));
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2019 Yuta Hirokawa (University of Tsukuba, Japan)
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <cbsl.h>
#define ARRAY_SIZE(X) (sizeof((X))/sizeof(*(X)))
#define CBSL_ERROR_CHECK(X) {if ((X) == cbsl_error) { fprintf(stderr, "error: %s\n", (#X)); }}
int a[1024]; /* 4 KiB */
double b[1024]; /* 8 KiB */
extern void save(void);
extern void load(void);
extern void rand_int(int, int*);
extern void rand_double(int, double*);
char cname[128];
int main(int argc, char** argv) {
srand((unsigned int)(time(NULL)));
sprintf(cname, "checkpoint.zst");
if (strcmp(argv[1],"-c") == 0)
{
rand_int(ARRAY_SIZE(a), a);
rand_double(ARRAY_SIZE(b), b);
save();
}
else if (strcmp(argv[1],"-r") == 0)
{
load();
}
else
{
return 1;
}
return 0;
}
void save(void) {
cbsl_ctx* ctx = cbsl_open(cbsl_store_mode, "./checkpoint.data");
if (ctx == NULL) {
fprintf(stderr, "error: cbsl_open save\n");
exit(1);
}
CBSL_ERROR_CHECK(cbsl_write(ctx, a, sizeof(a)));
CBSL_ERROR_CHECK(cbsl_write(ctx, b, sizeof(b)));
CBSL_ERROR_CHECK(cbsl_close(ctx));
printf("before compressed a[%d] = %d\n", 124, a[124]);
printf("before compressed b[%d] = %e\n", 514, b[514]);
}
void load(void) {
cbsl_ctx* ctx = cbsl_open(cbsl_load_mode, "./checkpoint.data");
if (ctx == NULL) {
fprintf(stderr, "error: cbsl_open load\n");
exit(1);
}
CBSL_ERROR_CHECK(cbsl_read(ctx, a, sizeof(a)));
CBSL_ERROR_CHECK(cbsl_read(ctx, b, sizeof(b)));
CBSL_ERROR_CHECK(cbsl_close(ctx));
printf("decompressed a[%d] = %d\n", 124, a[124]);
printf("decompressed b[%d] = %e\n", 514, b[514]);
}
void rand_int(int n, int *v) {
for(int i = 0; i < n; ++i)
v[i] = rand();
}
void rand_double(int n, double *v) {
for(int i = 0; i < n; ++i)
v[i] = 1.0 / (double)(rand());
}