dnp_sim3.c 39 KB

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