1
0

dnp_sim3.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* dnp_simulation.c - this file varifies the correctness of dnp algorithm
  13. by generating random sequences of operations, applying
  14. the algorithm and outputing the result
  15. usage: dnp_sim [-h] [-n <number of simulations> ] [-v] [-f <output file>]
  16. -h - print usage information.
  17. -n <number of simulations> - how many simulations to perform; default - 1.
  18. -v - verbose mode (prints full entry state after each operation execution)
  19. -f <output file> - file where results are stored; by default results are
  20. printed to the screen.
  21. -o <op file> - file that contains operation sequence to execute; by default,
  22. random sequence is generated.
  23. */
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include <memory.h>
  28. #include <string.h>
  29. #include <time.h>
  30. #include <windows.h>
  31. #define MAX_OPS 18 /* maximum number of operations in a simulation */
  32. #define MAX_VALS 10 /* maximum number of values is entry or dn */
  33. #define MAX_ATTR_NAME 16 /* max length of the attribute name */
  34. #define NOT_PRESENT -1
  35. #define SV_ATTR_NAME "sv_attr" /* name of the singlevalued attribute */
  36. #define MV_ATTR_NAME "mv_attr" /* name of the multivalued attribute */
  37. /* data types */
  38. /* value */
  39. typedef struct value_state
  40. {
  41. int value_index; /* value */
  42. int presence_csn; /* last time at which we know the value was present */
  43. int delete_csn; /* last attempt to delete this value */
  44. int present; /* flag that tells whether the value is present */
  45. } Value_State;
  46. /* shared attribute state */
  47. typedef struct attr_state
  48. {
  49. int delete_csn; /* last deletion csn */
  50. int present; /* flag that tells whether the attribute is present */
  51. }Attr_State;
  52. /* singlevalued attribute */
  53. typedef struct sv_attr_state
  54. {
  55. Attr_State attr_state; /* shared attribute state */
  56. Value_State current_value; /* current attribute value */
  57. Value_State *pending_value; /* latest pending value */
  58. } SV_Attr_State;
  59. /* maltivalued attribute */
  60. typedef struct mv_attr_state
  61. {
  62. Attr_State attr_state; /* shared attribute state */
  63. Value_State values [MAX_VALS]; /* latest pending value */
  64. int value_count; /* number of values in the array */
  65. } MV_Attr_State;
  66. /* node of dn_csn_list */
  67. typedef struct dn_csn
  68. {
  69. int csn; /* dn csn */
  70. int sv_attr; /* is this single valued or multivalued attr */
  71. int value_index; /* dn value */
  72. } Dn_Csn;
  73. typedef struct entry_state
  74. {
  75. Dn_Csn dn_csns [MAX_VALS + 1]; /* list of dn csns for this entry */
  76. int dn_csn_count; /* csn of the current dn */
  77. SV_Attr_State sv_attr; /* singlevalued attribute */
  78. MV_Attr_State mv_attr; /* singlevalued attribute */
  79. } Entry_State;
  80. typedef enum
  81. {
  82. OP_ADD_VALUE,
  83. OP_DELETE_VALUE,
  84. OP_RENAME_ENTRY,
  85. OP_DELETE_ATTR,
  86. OP_END
  87. } Operation_Type;
  88. typedef struct operation
  89. {
  90. Operation_Type type; /* operation type */
  91. int csn; /* operation csn */
  92. int sv_attr; /* is this applied to singlevalued attribute */
  93. int value_index; /* value to add, remove or rename from */
  94. int delete_old_rdn; /* rename only */
  95. int old_rdn_sv_attr; /* is oldrdn a singlevalued attribute */
  96. int old_rdn_value_index; /* index of old_rdn */
  97. }Operation;
  98. typedef struct simulator_params
  99. {
  100. int runs; /* number of runs */
  101. FILE *fout; /* output file */
  102. int value_count; /* number of values */
  103. int verbose; /* verbose mode */
  104. Operation *ops; /* operation sequence to apply */
  105. int op_count;
  106. }Simulator_Params;
  107. /* gloabl data */
  108. Simulator_Params sim;
  109. char *g_values[] =
  110. {
  111. "v",
  112. "u",
  113. "w",
  114. NULL
  115. };
  116. /* forward declarations */
  117. /* initialization */
  118. void process_cmd (int argc, char **argv);
  119. void set_default_sim_params ();
  120. int count_values ();
  121. void parse_operations_file (char *name);
  122. void parse_operation (char *line, int pos);
  123. int value2index (char *value);
  124. void print_usage ();
  125. /* simulation run */
  126. void run_simulation ();
  127. void generate_operations (Operation **ops, int *op_count);
  128. void generate_operation (Operation *op, int csn);
  129. int* generate_operation_order (int op_count, int seq_num);
  130. void apply_operation_sequence (Operation *ops, int op_count, int *order, Entry_State *entry);
  131. void init_entry_state (Entry_State *entry);
  132. void init_sv_attr_state (SV_Attr_State *sv_attr);
  133. void init_mv_attr_state (MV_Attr_State *mv_attr);
  134. void init_value_state (Value_State *val, int seq_num);
  135. void free_operations (Operation **ops);
  136. int ** new_perm_table (int op_count);
  137. void free_perm_table (int ***perm_table, int op_count);
  138. int get_perm_count (int op_count);
  139. void generate_perm_table (int *elements, int element_count, int static_part,
  140. int **perm_table);
  141. void apply_operation (Entry_State *entry, Operation *op);
  142. void apply_add_operation (Entry_State *entry, Operation *op);
  143. void apply_value_delete_operation (Entry_State *entry, Operation *op);
  144. void apply_attr_delete_operation (Entry_State *entry, Operation *op);
  145. void apply_rename_operation (Entry_State *entry, Operation *op);
  146. void resolve_mv_attr_state (Entry_State *entry, Value_State *value);
  147. void resolve_sv_attr_state (Entry_State *entry, Value_State *value);
  148. void purge_sv_attr_state (Entry_State *entry);
  149. void purge_mv_attr_state (Entry_State *entry, Value_State *value);
  150. int value_distinguished_at_csn (Entry_State *entry, int sv_attr, Value_State *value, int csn);
  151. /* state comparison */
  152. int compare_entry_state (Entry_State *entry1, Entry_State *entry2, int run);
  153. int compare_entry_state_quick (Entry_State *entry1, Entry_State *entry2, int run);
  154. int compare_sv_attr_state_quick (SV_Attr_State *sv_attr1, SV_Attr_State *sv_attr2, int run);
  155. int compare_mv_attr_state_quick (MV_Attr_State *mv_attr1, MV_Attr_State *mv_attr2, int run);
  156. int compare_sv_attr_state (SV_Attr_State *sv_attr1, SV_Attr_State *sv_attr2, int run);
  157. int compare_mv_attr_state (MV_Attr_State *mv_attr1, MV_Attr_State *mv_attr2, int run);
  158. int compare_value_state (Value_State *value1, Value_State *value2, int run);
  159. /* dnc_csn handling */
  160. int dn_csn_add (Entry_State *entry, int sv_attr, int value_index, int csn);
  161. /* data tracing */
  162. void dump_operations (Operation *ops, int op_count, int *order);
  163. void dump_operation (Operation *op);
  164. void dump_perm_table (int **perm_table, int op_count);
  165. void dump_entry_state (Entry_State *entry);
  166. void dump_sv_attr_state (SV_Attr_State *sv_attr);
  167. void dump_mv_attr_state (MV_Attr_State *mv_attr);
  168. void dump_value_state (Value_State *value, int sv_attr);
  169. void dump_dn_csn_list (Entry_State *entry);
  170. /* misc functions */
  171. int max_val (int a, int b);
  172. int main (int argc, char **argv)
  173. {
  174. int i;
  175. process_cmd (argc, argv);
  176. for (i = 0; i < sim.runs; i++)
  177. {
  178. fprintf (sim.fout, "*******running simulation #%d ...\n\n", i+1);
  179. run_simulation ();
  180. fprintf (sim.fout, "\n*******done with simulation #%d ...\n\n", i+1);
  181. }
  182. if (sim.fout != stdout)
  183. fclose (sim.fout);
  184. return 0;
  185. }
  186. void process_cmd (int argc, char **argv)
  187. {
  188. int i;
  189. set_default_sim_params ();
  190. if (argc == 1)
  191. {
  192. return;
  193. }
  194. if (strcmp (argv[1], "-h") == 0) /* print help */
  195. {
  196. print_usage ();
  197. exit (0);
  198. }
  199. i = 1;
  200. while (i < argc)
  201. {
  202. if (strcmp (argv[i], "-v") == 0) /* verbose mode */
  203. {
  204. sim.verbose = 1;
  205. i ++;
  206. }
  207. else if (strcmp (argv[i], "-n") == 0)
  208. {
  209. if (i < argc - 1)
  210. {
  211. int runs = atoi (argv[i + 1]);
  212. if (runs > 0)
  213. sim.runs = runs;
  214. i+=2;
  215. }
  216. else
  217. {
  218. /* ONREPL print warning */
  219. i++;
  220. }
  221. }
  222. else if (strcmp (argv[i], "-f") == 0) /* output file */
  223. {
  224. if (i < argc - 1)
  225. {
  226. FILE *f = fopen (argv[i + 1], "w");
  227. if (f == 0)
  228. {
  229. printf ("failed to open output file; error - %s, using stdout\n",
  230. strerror(errno));
  231. }
  232. else
  233. {
  234. /* ONREPL print warning */
  235. sim.fout = f;
  236. }
  237. i += 2;
  238. }
  239. else
  240. i++;
  241. }
  242. else if (strcmp (argv[i], "-o") == 0) /* file with operation sequence */
  243. {
  244. if (i < argc - 1)
  245. {
  246. parse_operations_file (argv[i+1]);
  247. i += 2;
  248. }
  249. else
  250. {
  251. /* ONREPL print warning */
  252. i ++;
  253. }
  254. }
  255. else /* unknown option */
  256. {
  257. printf ("unknown option - %s; ignored\n", argv[i]);
  258. i ++;
  259. }
  260. }
  261. }
  262. void set_default_sim_params ()
  263. {
  264. memset (&sim, 0, sizeof (sim));
  265. sim.runs = 1;
  266. sim.fout = stdout;
  267. sim.value_count = count_values ();
  268. }
  269. /* file format: <operation count>
  270. add <attribute> <value>
  271. delete <attribute>[ <value>]
  272. rename to <attribute> <value>[ delete <attribute> <value>]
  273. all spaces are significant
  274. */
  275. void parse_operations_file (char *name)
  276. {
  277. FILE *file = fopen (name, "r");
  278. char line [256];
  279. int i;
  280. if (file == NULL)
  281. {
  282. printf ("failed to open operations file %s: error = %d\n", name, errno);
  283. print_usage ();
  284. exit (1);
  285. }
  286. i = 0;
  287. while (fgets (line, sizeof (line), file))
  288. {
  289. if (i == 0)
  290. {
  291. /* read operation count */
  292. sim.op_count = atoi (line);
  293. if (sim.op_count < 1 || sim.op_count > MAX_OPS/2)
  294. {
  295. printf ("invalid operation count - %d; value must be between 1 and %d\n",
  296. sim.op_count, MAX_OPS/2);
  297. print_usage ();
  298. exit (1);
  299. }
  300. else
  301. {
  302. sim.ops = (Operation*)malloc (sim.op_count * sizeof (Operation));
  303. }
  304. }
  305. else
  306. {
  307. if (strlen (line) == 0) /* skip empty lines */
  308. continue;
  309. parse_operation (line, i);
  310. }
  311. i ++;
  312. }
  313. }
  314. #define ADD_KEYWORD "add "
  315. #define DELETE_KEYWORD "delete "
  316. #define RENAME_KEYWORD "rename to "
  317. #define DELET_OLD_RDN_KEYWORD " delete "
  318. void parse_operation (char *line, int i)
  319. {
  320. int rc = 0;
  321. char *pos;
  322. char buff [64];
  323. sim.ops [i - 1].csn = i;
  324. if (line[strlen(line) - 1] == '\n')
  325. line[strlen(line) - 1] = '\0';
  326. /* add <attribute> <value> */
  327. if (strncmp (line, ADD_KEYWORD, strlen (ADD_KEYWORD)) == 0)
  328. {
  329. sim.ops [i - 1].type = OP_ADD_VALUE;
  330. pos = strchr (&line[strlen (ADD_KEYWORD)], ' ');
  331. if (pos == NULL)
  332. {
  333. rc = -1;
  334. goto done;
  335. }
  336. memset (buff, 0, sizeof (buff));
  337. strncpy (buff, &line[strlen (ADD_KEYWORD)], pos - &line[strlen (ADD_KEYWORD)]);
  338. sim.ops [i - 1].sv_attr = strcmp (buff, MV_ATTR_NAME);
  339. sim.ops [i - 1].value_index = value2index (pos + 1);
  340. }
  341. /* delete <attribute>[ <value>] */
  342. else if (strncmp (line, DELETE_KEYWORD, strlen (DELETE_KEYWORD)) == 0)
  343. {
  344. pos = strchr (&line[strlen (DELETE_KEYWORD)], ' ');
  345. if (pos == NULL) /* delete attribute version */
  346. {
  347. sim.ops [i - 1].type = OP_DELETE_ATTR;
  348. sim.ops [i - 1].sv_attr = strcmp (&line[strlen (DELETE_KEYWORD)],
  349. MV_ATTR_NAME);
  350. }
  351. else /* delete value version */
  352. {
  353. memset (buff, 0, sizeof (buff));
  354. sim.ops [i - 1].type = OP_DELETE_VALUE;
  355. strncpy (buff, &line[strlen (DELETE_KEYWORD)],
  356. pos - &line[strlen (DELETE_KEYWORD)]);
  357. sim.ops [i - 1].sv_attr = strcmp (buff, MV_ATTR_NAME);
  358. sim.ops [i - 1].value_index = value2index (pos + 1);
  359. }
  360. }
  361. /* rename to <attribute> <valued>[ delete <attribute> <value>] */
  362. else if (strncmp (line, RENAME_KEYWORD, 10) == 0)
  363. {
  364. char *pos2;
  365. sim.ops [i - 1].type = OP_RENAME_ENTRY;
  366. pos = strchr (&line[strlen (RENAME_KEYWORD)], ' ');
  367. if (pos == NULL)
  368. {
  369. rc = -1;
  370. goto done;
  371. }
  372. memset (buff, 0, sizeof (buff));
  373. strncpy (buff, &line[strlen (RENAME_KEYWORD)], pos - &line[strlen (RENAME_KEYWORD)]);
  374. sim.ops [i - 1].sv_attr = strcmp (buff, MV_ATTR_NAME);
  375. pos2 = strstr (pos + 1, DELET_OLD_RDN_KEYWORD);
  376. if (pos2 == NULL) /* no delete old rdn part */
  377. {
  378. sim.ops [i - 1].value_index = value2index (pos + 1);
  379. sim.ops [i - 1].delete_old_rdn = 0;
  380. }
  381. else
  382. {
  383. memset (buff, 0, sizeof (buff));
  384. strncpy (buff, pos + 1, pos2 - pos - 1);
  385. sim.ops [i - 1].value_index = value2index (buff);
  386. pos2 += strlen (DELET_OLD_RDN_KEYWORD);
  387. pos = strchr (pos2, ' ');
  388. if (pos == NULL)
  389. {
  390. rc = -1;
  391. goto done;
  392. }
  393. memset (buff, 0, sizeof (buff));
  394. strncpy (buff, pos2, pos - pos2);
  395. sim.ops [i - 1].delete_old_rdn = 1;
  396. sim.ops [i - 1].old_rdn_sv_attr = strcmp (buff, MV_ATTR_NAME);
  397. sim.ops [i - 1].old_rdn_value_index = value2index (pos + 1);
  398. }
  399. }
  400. else
  401. {
  402. /* error */
  403. rc = -1;
  404. }
  405. done:
  406. if (rc)
  407. {
  408. /* invalid line */
  409. printf ("invalid operation: %s\n", line);
  410. exit (1);
  411. }
  412. }
  413. int value2index (char *value)
  414. {
  415. int i;
  416. for (i = 0; i < sim.value_count; i++)
  417. {
  418. if (strcmp (g_values[i], value) == 0)
  419. return i;
  420. }
  421. return -1;
  422. }
  423. void print_usage ()
  424. {
  425. printf ("usage: dnp_sim [-h] [-n <number of simulations> ] [-v] [-f <output file>]\n"
  426. "\t-h - print usage information\n"
  427. "\t-n <number of simulations>; default - 1\n"
  428. "\t-v - verbose mode\n"
  429. "\t-f <output file> - by default, results are printed to the screen\n"
  430. "\t-o <op file> - file that contains operation sequence to execute;\n"
  431. "\tby default, random sequence is generated.\n");
  432. }
  433. int count_values ()
  434. {
  435. int i;
  436. for (i = 0; g_values[i]; i++);
  437. return i;
  438. }
  439. void run_simulation ()
  440. {
  441. int *order;
  442. int i;
  443. int perm_count;
  444. Entry_State entry_first, entry_current;
  445. int error = 0;
  446. init_entry_state (&entry_first);
  447. fprintf (sim.fout, "initial entry state :\n");
  448. dump_entry_state (&entry_first);
  449. if (sim.ops == NULL)
  450. {
  451. generate_operations (&sim.ops, &sim.op_count);
  452. }
  453. fprintf (sim.fout, "initial operation set:\n");
  454. dump_operations (sim.ops, sim.op_count, NULL/* order */);
  455. perm_count = get_perm_count (sim.op_count);
  456. for (i = 0; i < perm_count; i++)
  457. {
  458. fprintf (sim.fout, "--------------------------------\n");
  459. fprintf (sim.fout, "simulation run %d\n", i + 1);
  460. fprintf (sim.fout, "--------------------------------\n");
  461. order = generate_operation_order (sim.op_count, i);
  462. if (i == 0)
  463. apply_operation_sequence (sim.ops, sim.op_count, order, &entry_first);
  464. else
  465. {
  466. apply_operation_sequence (sim.ops, sim.op_count, order, &entry_current);
  467. error |= compare_entry_state (&entry_first, &entry_current, i + 1);
  468. }
  469. }
  470. switch (error)
  471. {
  472. case 0: fprintf (sim.fout, "all runs left the entry in the same state\n");
  473. break;
  474. case 1: fprintf (sim.fout, "while value presence is consistent across all runs, "
  475. "the exact state does not match\n");
  476. break;
  477. case 3: fprintf (sim.fout, "the runs left entries in an inconsistent state\n");
  478. break;
  479. }
  480. free_operations (&sim.ops);
  481. }
  482. void generate_operations (Operation **ops, int *op_count)
  483. {
  484. int i;
  485. /* generate number operations in the sequence */
  486. *op_count = slapi_rand () % (MAX_OPS / 2) + 1;
  487. *ops = (Operation *)malloc (*op_count * sizeof (Operation));
  488. for (i = 0; i < *op_count; i ++)
  489. {
  490. generate_operation (&((*ops)[i]), i + 1);
  491. }
  492. }
  493. void generate_operation (Operation *op, int csn)
  494. {
  495. /* generate operation type */
  496. op->type = slapi_rand () % OP_END;
  497. op->csn = csn;
  498. /* choose if the operation applies to the single value or
  499. the multivalued attribute */
  500. op->sv_attr = slapi_rand () % 2;
  501. /* generate value to which operation applies */
  502. op->value_index = slapi_rand () % sim.value_count;
  503. if (op->type == OP_RENAME_ENTRY)
  504. {
  505. op->delete_old_rdn = slapi_rand () % 2;
  506. if (op->delete_old_rdn)
  507. {
  508. op->old_rdn_sv_attr = slapi_rand () % 2;
  509. op->old_rdn_value_index = slapi_rand () % sim.value_count;
  510. while (op->old_rdn_sv_attr == op->sv_attr &&
  511. op->old_rdn_value_index == op->value_index)
  512. {
  513. op->old_rdn_sv_attr = slapi_rand () % 2;
  514. op->old_rdn_value_index = slapi_rand () % sim.value_count;
  515. }
  516. }
  517. }
  518. }
  519. int* generate_operation_order (int op_count, int seq_num)
  520. {
  521. static int **perm_table = NULL;
  522. /* first request - generate pemutation table */
  523. if (seq_num == 0)
  524. {
  525. int elements [MAX_OPS];
  526. int i;
  527. if (perm_table)
  528. free_perm_table (&perm_table, op_count);
  529. perm_table = new_perm_table (op_count);
  530. for (i = 0; i < op_count; i++)
  531. elements [i] = i;
  532. generate_perm_table (elements, op_count, 0 /* static part */,
  533. perm_table);
  534. }
  535. return perm_table [seq_num];
  536. }
  537. void apply_operation_sequence (Operation *ops, int op_count, int *order, Entry_State *entry)
  538. {
  539. int i;
  540. init_entry_state (entry);
  541. if (!sim.verbose)
  542. {
  543. if (!sim.verbose)
  544. {
  545. fprintf (sim.fout, "operation_sequence for this run:\n");
  546. dump_operations (ops, op_count, order);
  547. }
  548. }
  549. for (i = 0; i < op_count; i++)
  550. {
  551. apply_operation (entry, &(ops [order[i]]));
  552. }
  553. if (!sim.verbose)
  554. {
  555. fprintf (sim.fout, "final entry state :\n");
  556. dump_entry_state (entry);
  557. }
  558. }
  559. void init_entry_state (Entry_State *entry)
  560. {
  561. memset (entry, 0, sizeof (*entry));
  562. entry->dn_csn_count = 1;
  563. init_sv_attr_state (&entry->sv_attr);
  564. init_mv_attr_state (&entry->mv_attr);
  565. }
  566. void init_sv_attr_state (SV_Attr_State *sv_attr)
  567. {
  568. memset (sv_attr, 0, sizeof (*sv_attr));
  569. sv_attr->attr_state.delete_csn = NOT_PRESENT;
  570. sv_attr->attr_state.present = 1;
  571. init_value_state (&sv_attr->current_value, 1);
  572. }
  573. void init_mv_attr_state (MV_Attr_State *mv_attr)
  574. {
  575. int i;
  576. memset (mv_attr, 0, sizeof (*mv_attr));
  577. mv_attr->attr_state.delete_csn = NOT_PRESENT;
  578. mv_attr->attr_state.present = 1;
  579. mv_attr->value_count = sim.value_count;
  580. for (i = 0; i < mv_attr->value_count; i++)
  581. {
  582. init_value_state (&(mv_attr->values[i]), i);
  583. }
  584. }
  585. void init_value_state (Value_State *val, int seq_num)
  586. {
  587. memset (val, 0, sizeof (*val));
  588. val->value_index = seq_num;
  589. val->present = 1;
  590. val->delete_csn = NOT_PRESENT;
  591. }
  592. void apply_operation (Entry_State *entry, Operation *op)
  593. {
  594. switch (op->type)
  595. {
  596. case OP_ADD_VALUE: apply_add_operation (entry, op);
  597. break;
  598. case OP_DELETE_VALUE: apply_value_delete_operation (entry, op);
  599. break;
  600. case OP_DELETE_ATTR: apply_attr_delete_operation (entry, op);
  601. break;
  602. case OP_RENAME_ENTRY: apply_rename_operation (entry, op);
  603. break;
  604. }
  605. if (sim.verbose)
  606. {
  607. fprintf (sim.fout, "operation: ");
  608. dump_operation (op);
  609. fprintf (sim.fout, "\n");
  610. dump_entry_state (entry);
  611. }
  612. }
  613. void free_operations (Operation **ops)
  614. {
  615. free (*ops);
  616. *ops = NULL;
  617. }
  618. int **new_perm_table (int op_count)
  619. {
  620. int i;
  621. int **perm_table;
  622. int perm_count = get_perm_count (op_count);
  623. perm_table = (int**)malloc (perm_count * sizeof (int*));
  624. for (i = 0; i < perm_count; i ++)
  625. perm_table [i] = (int*) malloc (op_count * sizeof (int));
  626. return perm_table;
  627. }
  628. void free_perm_table (int ***perm_table, int op_count)
  629. {
  630. int i;
  631. int perm_count = get_perm_count (op_count);
  632. for (i = 0; i < perm_count; i ++)
  633. free ((*perm_table)[i]);
  634. free (*perm_table);
  635. *perm_table = NULL;
  636. }
  637. void generate_perm_table (int *elements, int element_count, int static_part,
  638. int **perm_table)
  639. {
  640. int i;
  641. int elements_copy [MAX_OPS];
  642. int start_pos;
  643. if (element_count - 1 == static_part)
  644. {
  645. memcpy (*perm_table, elements, element_count * sizeof (int));
  646. return;
  647. }
  648. start_pos = 0;
  649. for (i = 0; i < element_count - static_part; i ++)
  650. {
  651. memcpy (elements_copy, elements, element_count * sizeof (int));
  652. elements_copy [static_part] = elements [static_part + i];
  653. elements_copy [static_part + i] = elements [static_part];
  654. generate_perm_table (elements_copy, element_count, static_part + 1,
  655. &perm_table [start_pos]);
  656. start_pos += get_perm_count (element_count - static_part - 1);
  657. }
  658. }
  659. int get_perm_count (int op_count)
  660. {
  661. int i;
  662. int perm_count = 1;
  663. for (i = 2; i <= op_count; i ++)
  664. perm_count *= i;
  665. return perm_count;
  666. }
  667. void apply_add_operation (Entry_State *entry, Operation *op)
  668. {
  669. if (op->sv_attr)
  670. {
  671. Value_State *val;
  672. Value_State temp_val;
  673. if (op->value_index == entry->sv_attr.current_value.value_index)
  674. {
  675. val = &entry->sv_attr.current_value;
  676. }
  677. else if (entry->sv_attr.pending_value &&
  678. op->value_index == entry->sv_attr.pending_value->value_index)
  679. {
  680. val = entry->sv_attr.pending_value;
  681. }
  682. else /* new value */
  683. {
  684. init_value_state (&temp_val, op->value_index);
  685. val = &temp_val;
  686. }
  687. if (val->presence_csn < op->csn)
  688. val->presence_csn = op->csn;
  689. resolve_sv_attr_state (entry, val);
  690. }
  691. else
  692. {
  693. if (entry->mv_attr.values[op->value_index].presence_csn < op->csn)
  694. {
  695. entry->mv_attr.values[op->value_index].presence_csn = op->csn;
  696. resolve_mv_attr_state (entry, &(entry->mv_attr.values[op->value_index]));
  697. }
  698. }
  699. }
  700. void apply_value_delete_operation (Entry_State *entry, Operation *op)
  701. {
  702. if (op->sv_attr)
  703. {
  704. if (entry->sv_attr.attr_state.delete_csn < op->csn)
  705. {
  706. entry->sv_attr.attr_state.delete_csn = op->csn;
  707. resolve_sv_attr_state (entry, NULL);
  708. }
  709. }
  710. else /* mv attr */
  711. {
  712. if (entry->mv_attr.values[op->value_index].delete_csn < op->csn)
  713. {
  714. entry->mv_attr.values[op->value_index].delete_csn = op->csn;
  715. resolve_mv_attr_state (entry, &(entry->mv_attr.values[op->value_index]));
  716. }
  717. }
  718. }
  719. void apply_attr_delete_operation (Entry_State *entry, Operation *op)
  720. {
  721. int i;
  722. if (op->sv_attr)
  723. {
  724. if (entry->sv_attr.attr_state.delete_csn < op->csn)
  725. {
  726. entry->sv_attr.attr_state.delete_csn = op->csn;
  727. resolve_sv_attr_state (entry, NULL);
  728. }
  729. }
  730. else /* mv attr */
  731. {
  732. if (entry->mv_attr.attr_state.delete_csn < op->csn)
  733. {
  734. entry->mv_attr.attr_state.delete_csn = op->csn;
  735. for (i = 0; i < sim.value_count; i++)
  736. {
  737. resolve_mv_attr_state (entry, &(entry->mv_attr.values[i]));
  738. }
  739. }
  740. }
  741. }
  742. void apply_rename_operation (Entry_State *entry, Operation *op)
  743. {
  744. int index;
  745. Operation del_op;
  746. /* insert new dn into dn_csn_list */
  747. index = dn_csn_add (entry, op->sv_attr, op->value_index, op->csn);
  748. /* issue delete value operation for the old rdn */
  749. if (op->delete_old_rdn)
  750. {
  751. del_op.type = OP_DELETE_VALUE;
  752. del_op.csn = op->csn;
  753. del_op.sv_attr = op->old_rdn_sv_attr;
  754. del_op.value_index = op->old_rdn_value_index;
  755. apply_value_delete_operation (entry, &del_op);
  756. }
  757. /* resolve state of the previous node in dn_csn_list */
  758. if (index > 0)
  759. {
  760. if (entry->dn_csns[index-1].sv_attr)
  761. {
  762. if (entry->dn_csns[index-1].value_index ==
  763. entry->sv_attr.current_value.value_index)
  764. {
  765. resolve_sv_attr_state (entry, &(entry->sv_attr.current_value));
  766. }
  767. else if (entry->sv_attr.pending_value &&
  768. entry->dn_csns[index-1].value_index ==
  769. entry->sv_attr.pending_value->value_index)
  770. {
  771. resolve_sv_attr_state (entry, entry->sv_attr.pending_value);
  772. }
  773. }
  774. else
  775. {
  776. int i = entry->dn_csns[index-1].value_index;
  777. resolve_mv_attr_state (entry, &(entry->mv_attr.values[i]));
  778. }
  779. }
  780. /* resolve state of the new dn */
  781. if (op->sv_attr)
  782. {
  783. Value_State *value;
  784. Value_State temp_val;
  785. if (op->value_index == entry->sv_attr.current_value.value_index)
  786. {
  787. value = &entry->sv_attr.current_value;
  788. }
  789. else if (entry->sv_attr.pending_value &&
  790. op->value_index == entry->sv_attr.pending_value->value_index)
  791. {
  792. value = entry->sv_attr.pending_value;
  793. }
  794. else /* new value */
  795. {
  796. init_value_state (&temp_val, op->value_index);
  797. value = &temp_val;
  798. }
  799. if (value->presence_csn == NOT_PRESENT || value->presence_csn < op->csn)
  800. value->presence_csn = op->csn;
  801. resolve_sv_attr_state (entry, value);
  802. }
  803. else
  804. {
  805. if (entry->mv_attr.values[op->value_index].presence_csn == NOT_PRESENT ||
  806. entry->mv_attr.values[op->value_index].presence_csn < op->csn)
  807. entry->mv_attr.values[op->value_index].presence_csn = op->csn;
  808. resolve_mv_attr_state (entry, &(entry->mv_attr.values[op->value_index]));
  809. }
  810. }
  811. void purge_mv_attr_state (Entry_State *entry, Value_State *value)
  812. {
  813. if (value->presence_csn > value->delete_csn)
  814. value->delete_csn = NOT_PRESENT;
  815. }
  816. void purge_sv_attr_state (Entry_State *entry)
  817. {
  818. if (entry->sv_attr.attr_state.delete_csn != NOT_PRESENT)
  819. {
  820. if (entry->sv_attr.pending_value)
  821. {
  822. if (entry->sv_attr.attr_state.delete_csn <
  823. entry->sv_attr.pending_value->presence_csn)
  824. {
  825. entry->sv_attr.attr_state.delete_csn = NOT_PRESENT;
  826. }
  827. }
  828. else
  829. {
  830. if (entry->sv_attr.attr_state.delete_csn <
  831. entry->sv_attr.current_value.presence_csn)
  832. entry->sv_attr.attr_state.delete_csn = NOT_PRESENT;
  833. }
  834. }
  835. }
  836. void resolve_mv_attr_state (Entry_State *entry, Value_State *value)
  837. {
  838. purge_mv_attr_state (entry, value);
  839. /* no deletes that effect the state */
  840. if (max_val (value->delete_csn, entry->mv_attr.attr_state.delete_csn) <
  841. value->presence_csn)
  842. {
  843. value->present = 1;
  844. return;
  845. }
  846. if (value->present) /* check if it should be removed based on the current state */
  847. {
  848. if (!value_distinguished_at_csn (entry, 0, value,
  849. max (value->delete_csn, entry->mv_attr.attr_state.delete_csn)))
  850. {
  851. value->present = 0;
  852. }
  853. }
  854. else /* not present - check if it should be restored */
  855. {
  856. if (value_distinguished_at_csn (entry, 0, value,
  857. max (value->delete_csn, entry->mv_attr.attr_state.delete_csn)))
  858. {
  859. value->present = 1;
  860. }
  861. }
  862. if (entry->mv_attr.attr_state.delete_csn == NOT_PRESENT)
  863. {
  864. entry->mv_attr.attr_state.present = 1;
  865. }
  866. else
  867. {
  868. int i;
  869. int distinguished = 0;
  870. for (i = 0; i < entry->mv_attr.value_count; i ++)
  871. {
  872. distinguished |= value_distinguished_at_csn (entry, 0,
  873. &(entry->mv_attr.values[i]),
  874. entry->mv_attr.attr_state.delete_csn);
  875. }
  876. entry->mv_attr.attr_state.present = distinguished;
  877. }
  878. }
  879. void resolve_sv_attr_state (Entry_State *entry, Value_State *value)
  880. {
  881. purge_sv_attr_state (entry);
  882. if (value)
  883. {
  884. /* existing value is modified */
  885. if (value == &(entry->sv_attr.current_value) ||
  886. value == entry->sv_attr.pending_value)
  887. {
  888. /* check if current value should be replaced with the pending value */
  889. if (entry->sv_attr.pending_value)
  890. {
  891. if (!value_distinguished_at_csn (entry, 1, &entry->sv_attr.current_value,
  892. entry->sv_attr.current_value.presence_csn))
  893. {
  894. /* replace current value with the pending value */
  895. memcpy (&entry->sv_attr.current_value, entry->sv_attr.pending_value,
  896. sizeof (Value_State));
  897. free (entry->sv_attr.pending_value);
  898. entry->sv_attr.pending_value = NULL;
  899. }
  900. }
  901. }
  902. else /* addition of a new value */
  903. {
  904. /* new value is before the current value; note that, for new value,
  905. presence_csn is the same as distinguished_csn */
  906. if (value->presence_csn < entry->sv_attr.current_value.presence_csn)
  907. {
  908. /* if new value is distinguished, it should become current and the
  909. current can become pending */
  910. if (value_distinguished_at_csn (entry, 1, value,
  911. entry->sv_attr.current_value.presence_csn))
  912. {
  913. if (entry->sv_attr.pending_value == NULL)
  914. {
  915. entry->sv_attr.pending_value = (Value_State*)
  916. malloc (sizeof (Value_State));
  917. memcpy (entry->sv_attr.pending_value, &entry->sv_attr.current_value,
  918. sizeof (Value_State));
  919. }
  920. memcpy (&entry->sv_attr.current_value, value, sizeof (Value_State));
  921. }
  922. }
  923. else /* new value is after the current value */
  924. {
  925. /* if current value is not distinguished, new value should
  926. become distinguished */
  927. if (!value_distinguished_at_csn (entry, 1, &entry->sv_attr.current_value,
  928. value->presence_csn))
  929. {
  930. memcpy (&entry->sv_attr.current_value, value, sizeof (Value_State));
  931. }
  932. else /* current value is distinguished - check if new value should replace
  933. the pending value */
  934. { if (entry->sv_attr.pending_value)
  935. {
  936. if (value->presence_csn > entry->sv_attr.pending_value->presence_csn)
  937. {
  938. memcpy (entry->sv_attr.pending_value, value, sizeof (Value_State));
  939. }
  940. }
  941. else
  942. {
  943. entry->sv_attr.pending_value = (Value_State*)malloc (sizeof (Value_State));
  944. memcpy (entry->sv_attr.pending_value, value, sizeof (Value_State));
  945. }
  946. }
  947. }
  948. }
  949. }
  950. /* update the attribute state */
  951. purge_sv_attr_state (entry);
  952. /* set attribute state */
  953. if (entry->sv_attr.attr_state.delete_csn != NOT_PRESENT &&
  954. !value_distinguished_at_csn (entry, 1, &entry->sv_attr.current_value,
  955. entry->sv_attr.attr_state.delete_csn))
  956. {
  957. entry->sv_attr.attr_state.present = 0;
  958. }
  959. else
  960. {
  961. entry->sv_attr.attr_state.present = 1;
  962. }
  963. }
  964. int value_distinguished_at_csn (Entry_State *entry, int sv_attr, Value_State *value, int csn)
  965. {
  966. int i;
  967. for (i = 0; i < entry->dn_csn_count; i++)
  968. {
  969. if (entry->dn_csns[i].csn > csn)
  970. break;
  971. }
  972. /* i is never equal to 0 because the csn of the first element is always
  973. smaller than csn of any operation we can receive */
  974. return (entry->dn_csns[i-1].value_index == value->value_index &&
  975. entry->dn_csns[i-1].sv_attr == sv_attr);
  976. }
  977. int compare_entry_state (Entry_State *entry1, Entry_State *entry2, int run)
  978. {
  979. int i;
  980. int error = 0;
  981. error = compare_entry_state_quick (entry1, entry2, run);
  982. if (error)
  983. return 3;
  984. /* compare dnc_csn list */
  985. if (entry1->dn_csn_count != entry2->dn_csn_count)
  986. {
  987. fprintf (sim.fout, "dn_csn count is %d for run 1 and %d for run %d\n",
  988. entry1->dn_csn_count, entry2->dn_csn_count, run);
  989. error = 1;
  990. }
  991. for (i = 0; i < entry1->dn_csn_count; i++)
  992. {
  993. if (entry1->dn_csns [i].csn != entry2->dn_csns [i].csn ||
  994. entry1->dn_csns [i].sv_attr != entry2->dn_csns [i].sv_attr ||
  995. entry1->dn_csns [i].value_index != entry2->dn_csns [i].value_index)
  996. {
  997. fprintf (sim.fout,"elements %d of dn csn list are different:\n"
  998. "\tfirst run: csn - %d, attr - %s, value - %s\n"
  999. "\t%d run: csn - %d, attr - %s value - %s\n", i,
  1000. entry1->dn_csns [i].csn,
  1001. entry1->dn_csns [i].sv_attr ? SV_ATTR_NAME : MV_ATTR_NAME,
  1002. g_values[entry1->dn_csns [i].value_index],
  1003. run, entry2->dn_csns [i].csn,
  1004. entry2->dn_csns [i].sv_attr ? SV_ATTR_NAME : MV_ATTR_NAME,
  1005. g_values[entry2->dn_csns [i].value_index]);
  1006. error = 1;
  1007. }
  1008. }
  1009. error |= compare_sv_attr_state (&entry1->sv_attr, &entry2->sv_attr, run);
  1010. error |= compare_mv_attr_state (&entry1->mv_attr, &entry2->mv_attr, run);
  1011. if (error != 0)
  1012. {
  1013. return 1;
  1014. }
  1015. else
  1016. return 0;
  1017. }
  1018. /* just compare if the same attributes and values are present */
  1019. int compare_entry_state_quick (Entry_State *entry1, Entry_State *entry2, int run)
  1020. {
  1021. int error;
  1022. error = compare_sv_attr_state_quick (&entry1->sv_attr, &entry2->sv_attr, run);
  1023. error |= compare_mv_attr_state_quick (&entry1->mv_attr, &entry2->mv_attr, run);
  1024. return error;
  1025. }
  1026. int compare_sv_attr_state_quick (SV_Attr_State *sv_attr1, SV_Attr_State *sv_attr2, int run)
  1027. {
  1028. int error = 0;
  1029. if (sv_attr1->attr_state.present != sv_attr2->attr_state.present)
  1030. {
  1031. fprintf (sim.fout, "singlevalued attribute is %s present in the first run "
  1032. "but is %s present in the %d run\n",
  1033. sv_attr1->attr_state.present ? "" : "not",
  1034. sv_attr2->attr_state.present ? "" : "not", run);
  1035. return 1;
  1036. }
  1037. if (sv_attr1->attr_state.present &&
  1038. sv_attr1->current_value.value_index != sv_attr2->current_value.value_index)
  1039. {
  1040. fprintf (sim.fout, "different values for singlevalued attribute: %s for the \n"
  1041. "first run and %s for the %d run\n",
  1042. g_values [sv_attr1->current_value.value_index],
  1043. g_values [sv_attr2->current_value.value_index], run);
  1044. return 1;
  1045. }
  1046. return 0;
  1047. }
  1048. int compare_mv_attr_state_quick (MV_Attr_State *mv_attr1, MV_Attr_State *mv_attr2, int run)
  1049. {
  1050. int i;
  1051. int error = 0;
  1052. if (mv_attr1->attr_state.present != mv_attr2->attr_state.present)
  1053. {
  1054. fprintf (sim.fout, "multivalued attribute is %s present in the first run "
  1055. "but is %s present in the %d run\n",
  1056. mv_attr1->attr_state.present ? "" : "not",
  1057. mv_attr2->attr_state.present ? "" : "not", run);
  1058. return 1;
  1059. }
  1060. /* value count does not change during the iteration, so we don't have
  1061. to check if the count is the same for both attributes */
  1062. for (i = 0; i < mv_attr1->value_count; i++)
  1063. {
  1064. if (mv_attr1->values[i].present != mv_attr2->values[i].present)
  1065. {
  1066. fprintf (sim.fout, "value %s is %s present in the multivalued attribute\n"
  1067. "in the first run but %s present in the %d run\n",
  1068. g_values[i], mv_attr1->values[i].present ? "" : "not",
  1069. mv_attr2->values[i].present ? "" : "not", run);
  1070. error = 1;
  1071. }
  1072. }
  1073. return error;
  1074. }
  1075. int compare_sv_attr_state (SV_Attr_State *sv_attr1, SV_Attr_State *sv_attr2, int run)
  1076. {
  1077. int error = 0;
  1078. if (sv_attr1->attr_state.delete_csn != sv_attr2->attr_state.delete_csn)
  1079. {
  1080. fprintf (sim.fout, "singlevalued attribute deletion csn is %d for run 1 "
  1081. "but is %d for run %d\n", sv_attr1->attr_state.delete_csn,
  1082. sv_attr2->attr_state.delete_csn, run);
  1083. error = 1;
  1084. }
  1085. error |= compare_value_state (&sv_attr1->current_value, &sv_attr2->current_value, run);
  1086. if ((sv_attr1->pending_value && !sv_attr1->pending_value) ||
  1087. (!sv_attr1->pending_value && sv_attr1->pending_value))
  1088. {
  1089. fprintf (sim.fout, "pending value is %s present in the singlevalued attribute\n"
  1090. " in the first run but is %s in the %d run\n",
  1091. sv_attr1->pending_value ? "" : "not",
  1092. sv_attr2->pending_value ? "" : "not", run);
  1093. return 1;
  1094. }
  1095. if (sv_attr1->pending_value)
  1096. error |= compare_value_state (sv_attr1->pending_value, sv_attr2->pending_value, run);
  1097. return 0;
  1098. }
  1099. int compare_mv_attr_state (MV_Attr_State *mv_attr1, MV_Attr_State *mv_attr2, int run)
  1100. {
  1101. int error = 0;
  1102. int i;
  1103. if (mv_attr1->attr_state.delete_csn != mv_attr2->attr_state.delete_csn)
  1104. {
  1105. fprintf (sim.fout, "multivalued attribute deletion csn is %d for run 1 "
  1106. "but is %d for run %d\n", mv_attr1->attr_state.delete_csn,
  1107. mv_attr2->attr_state.delete_csn, run);
  1108. error = 1;
  1109. }
  1110. for (i = 0; i < mv_attr1->value_count; i++)
  1111. {
  1112. error |= compare_value_state (&mv_attr1->values[i], &mv_attr2->values[i], run);
  1113. }
  1114. return error;
  1115. }
  1116. int compare_value_state (Value_State *value1, Value_State *value2, int run)
  1117. {
  1118. int error = 0;
  1119. if (value1->presence_csn != value2->presence_csn)
  1120. {
  1121. fprintf (sim.fout, "multivalued attribute: presence csn for value %s is %d "
  1122. "in run 1 but is %d in run %d\n", g_values[value1->value_index],
  1123. value1->presence_csn, value2->presence_csn, run);
  1124. error = 1;
  1125. }
  1126. if (value1->delete_csn != value2->delete_csn)
  1127. {
  1128. fprintf (sim.fout, "multivalued attribute: delete csn for value %s is %d in run 1 "
  1129. "but is %d in run %d\n", g_values[value1->value_index],
  1130. value1->delete_csn, value2->delete_csn, run);
  1131. error = 1;
  1132. }
  1133. return error;
  1134. }
  1135. int dn_csn_add (Entry_State *entry, int sv_attr, int value_index, int csn)
  1136. {
  1137. int i;
  1138. for (i = 0; i < entry->dn_csn_count; i++)
  1139. {
  1140. if (entry->dn_csns[i].csn > csn)
  1141. break;
  1142. }
  1143. if (i < entry->dn_csn_count)
  1144. {
  1145. memcpy (&(entry->dn_csns[i+1]), &(entry->dn_csns[i]),
  1146. (entry->dn_csn_count - i) * sizeof (Dn_Csn));
  1147. }
  1148. entry->dn_csns[i].csn = csn;
  1149. entry->dn_csns[i].sv_attr = sv_attr;
  1150. entry->dn_csns[i].value_index = value_index;
  1151. entry->dn_csn_count ++;
  1152. return i;
  1153. }
  1154. void dump_operations (Operation *ops, int op_count, int *order)
  1155. {
  1156. int index;
  1157. int i;
  1158. for (i = 0; i < op_count; i ++)
  1159. {
  1160. if (order == NULL) /* current order */
  1161. index = i;
  1162. else
  1163. index = order [i];
  1164. dump_operation (&ops[index]);
  1165. }
  1166. fprintf (sim.fout, "\n");
  1167. }
  1168. void dump_operation (Operation *op)
  1169. {
  1170. switch (op->type)
  1171. {
  1172. case OP_ADD_VALUE:
  1173. fprintf (sim.fout, "\t%d add value %s to %s\n", op->csn,
  1174. g_values [op->value_index],
  1175. op->sv_attr ? SV_ATTR_NAME : MV_ATTR_NAME);
  1176. break;
  1177. case OP_DELETE_VALUE:
  1178. fprintf (sim.fout, "\t%d delete value %s from %s\n", op->csn,
  1179. g_values [op->value_index],
  1180. op->sv_attr ? SV_ATTR_NAME : MV_ATTR_NAME);
  1181. break;
  1182. case OP_DELETE_ATTR:
  1183. fprintf (sim.fout, "\t%d delete %s attribute\n", op->csn,
  1184. op->sv_attr ? SV_ATTR_NAME : MV_ATTR_NAME);
  1185. break;
  1186. case OP_RENAME_ENTRY:
  1187. fprintf (sim.fout, "\t%d rename entry to %s=%s", op->csn,
  1188. op->sv_attr ? SV_ATTR_NAME : MV_ATTR_NAME,
  1189. g_values [op->value_index]);
  1190. if (op->delete_old_rdn)
  1191. fprintf (sim.fout, " delete old rdn %s=%s\n",
  1192. op->old_rdn_sv_attr ? SV_ATTR_NAME : MV_ATTR_NAME,
  1193. g_values [op->old_rdn_value_index]);
  1194. else
  1195. fprintf (sim.fout, "\n");
  1196. break;
  1197. }
  1198. }
  1199. void dump_perm_table (int **perm_table, int op_count)
  1200. {
  1201. int i, j;
  1202. int perm_count = get_perm_count (op_count);
  1203. for (i = 0; i < op_count; i++)
  1204. {
  1205. for (j = 0; j < perm_count; j++)
  1206. {
  1207. fprintf (sim.fout, "%d ", perm_table [j][i]);
  1208. }
  1209. fprintf (sim.fout, "\n");
  1210. }
  1211. }
  1212. void dump_entry_state (Entry_State *entry)
  1213. {
  1214. dump_dn_csn_list (entry);
  1215. dump_sv_attr_state (&entry->sv_attr);
  1216. dump_mv_attr_state (&entry->mv_attr);
  1217. fprintf (sim.fout, "\n");
  1218. }
  1219. void dump_sv_attr_state (SV_Attr_State *sv_attr)
  1220. {
  1221. fprintf (sim.fout, "\tattribute %s is %s present", SV_ATTR_NAME,
  1222. sv_attr->attr_state.present ? "" : "not");
  1223. if (sv_attr->attr_state.present)
  1224. {
  1225. fprintf (sim.fout, " and has the value of %s\n",
  1226. g_values[sv_attr->current_value.value_index]);
  1227. }
  1228. else
  1229. {
  1230. fprintf (sim.fout, "\n");
  1231. }
  1232. if (sim.verbose)
  1233. {
  1234. fprintf (sim.fout, "\t\tdeletion csn: %d\n", sv_attr->attr_state.delete_csn);
  1235. fprintf (sim.fout, "\t\tcurrent value: ");
  1236. dump_value_state (&sv_attr->current_value, 1/* for single valued attr */);
  1237. if (sv_attr->pending_value)
  1238. {
  1239. fprintf (sim.fout, "\t\tpending value: ");
  1240. dump_value_state (sv_attr->pending_value, 1/* for single valued attr */);
  1241. }
  1242. }
  1243. }
  1244. void dump_mv_attr_state (MV_Attr_State *mv_attr)
  1245. {
  1246. int i;
  1247. fprintf (sim.fout, "\tattribute %s is %s present\n", MV_ATTR_NAME,
  1248. mv_attr->attr_state.present ? "" : "not");
  1249. if (sim.verbose)
  1250. {
  1251. fprintf (sim.fout, "\t\tdeletion csn: %d\n", mv_attr->attr_state.delete_csn);
  1252. }
  1253. for (i = 0; i < mv_attr->value_count; i++)
  1254. {
  1255. dump_value_state (&(mv_attr->values[i]), 0);
  1256. }
  1257. }
  1258. void dump_value_state (Value_State *value, int sv_attr)
  1259. {
  1260. if (!sv_attr)
  1261. {
  1262. fprintf (sim.fout, "\tvalue %s is %s present\n", g_values[value->value_index],
  1263. value->present ? "" : "not");
  1264. }
  1265. else
  1266. {
  1267. fprintf (sim.fout, "%s\n", g_values[value->value_index]);
  1268. }
  1269. if (sim.verbose)
  1270. {
  1271. fprintf (sim.fout, "\t\t\tpresence csn: %d\n", value->presence_csn);
  1272. fprintf (sim.fout, "\t\t\tdeletion value csn: %d\n", value->delete_csn);
  1273. }
  1274. }
  1275. void dump_dn_csn_list (Entry_State *entry)
  1276. {
  1277. int i;
  1278. fprintf (sim.fout, "\tdn csn list: \n");
  1279. for (i = 0; i < entry->dn_csn_count; i++)
  1280. {
  1281. fprintf (sim.fout, "\t\t %s=%s, csn: %d\n",
  1282. entry->dn_csns[i].sv_attr ? SV_ATTR_NAME : MV_ATTR_NAME,
  1283. g_values[entry->dn_csns[i].value_index], entry->dn_csns[i].csn);
  1284. }
  1285. }
  1286. /* misc functions */
  1287. int max_val (int a, int b)
  1288. {
  1289. if (a >= b)
  1290. return a;
  1291. else
  1292. return b;
  1293. }